Skip to content
Development and deployment2 min read

What is WebSocket?

Short answer

WebSocket is a protocol that opens a persistent, two-way channel between a browser and a server over a single TCP connection. It starts as an ordinary HTTP request that gets upgraded, after which either side can send messages instantly: ideal for chat, live notifications and dashboards.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. How it works
  2. Example: a browser client
  3. Common problems
  4. On ArabyHost

With plain HTTP, the browser asks, the server answers, and that’s the end of it. To learn that a new message had arrived, the browser used to ask every few seconds (polling), which wastes resources and delays messages. The WebSocket standard, RFC 6455, was published in 2011 to solve that.

How it works

  1. Handshake: the browser sends an HTTP request with Upgrade: websocket, Connection: Upgrade and a Sec-WebSocket-Key.
  2. The server replies with 101 Switching Protocols.
  3. The TCP connection stays open, and both sides exchange text or binary frames whenever they like.
  4. One side sends a close frame or the connection drops, and the client reconnects.
Plain HTTP WebSocket
Who starts a message Always the client Either side
Connection Request, then response Persistent
URL scheme https:// wss://
Good for Pages and APIs Chat, games, live dashboards

Example: a browser client

const ws = new WebSocket("wss://example.com/chat");

ws.onopen = () => ws.send(JSON.stringify({ type: "join", room: "support" }));
ws.onmessage = (event) => console.log("message:", event.data);
ws.onclose = () => console.log("disconnected, retry shortly");

On the server side, libraries such as ws or Socket.IO are common in Node.js. Note that Socket.IO is its own protocol on top of WebSocket, so its client can’t talk to a plain WebSocket server.

Common problems

  • Behind a proxy or CDN: the intermediary must pass the Upgrade header through, or the handshake fails.
  • Drops after idle timeouts: fix with periodic ping/pong messages.
  • Mixed content: an HTTPS page can’t open ws://: see mixed content.
  • A long-running process: a WebSocket server must stay running and hold connections in memory, unlike traditional PHP, which ends with each request.

On ArabyHost

Clients that connect out to an external WebSocket service (a Discord bot connecting to Discord’s gateway, for example) run on AI/Developer plans as background processes kept alive by cron; see the Discord bot guide. Running a public WebSocket server that accepts visitor connections depends on the app and your account’s resource limits, so talk to us before relying on it. For simple features like notifications, you can start with Server-Sent Events or periodic polling of an API.

FAQ

What's the difference between ws:// and wss://?

ws:// is unencrypted; wss:// is encrypted with TLS, just like HTTPS. Always use wss:// on public sites, because browsers block ws:// from HTTPS pages.

Do I need WebSocket for every real-time feature?

No. If data only flows from server to browser, such as notifications or streaming an AI model's reply word by word, Server-Sent Events are simpler and run over plain HTTP.

Why does my WebSocket connection drop after a few quiet minutes?

Proxies and firewalls close idle connections after a timeout. Send periodic ping/pong heartbeats and build reconnect logic into the client.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.