Skip to content

ESP32 WebSocket Client Cannot Connect

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about ESP32 WebSocket Client Cannot Connect. We cover key concepts, practical examples, and best practices.

The Problem

ESP32 WebSocket client fails to connect or disconnects immediately.

Quick Fix

Wrong

WebSocketClient ws;
ws.begin("192.168.1.100", 8080, "/ws");
WebSocket connection failed. No handshake response.
WebSocketClient ws;
WiFiClient client;
ws.setExtraHeaders("Origin: http://esp32.local");
if (ws.begin(client, "ws://192.168.1.100:8080/ws")) {
  Serial.println("WebSocket connected");
  ws.send("Hello server");
}
WebSocket connected
Message sent to server

Prevention

Use correct URL format (ws:// or wss://). Set proper headers (Origin). Ensure server supports RFC 6455. Add heartbeat mechanism. Handle disconnection with auto-reconnect.

DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.

FAQ

### What WebSocket version does ESP32 use?

ESP32 WebSocket uses RFC 6455 protocol version 13, the standard supported by most servers.

Can ESP32 use secure WebSocket (WSS)?

Yes. Use WiFiClientSecure and wss:// URL with the server CA certificate.

How do I keep WebSocket alive?

Send a ping frame every 30 seconds. Many servers close idle connections after 60 seconds.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro