VIP supports WebSocket connections for Node.js environments. The WebSocket API allows long-running, two-way communication between a client and a server. WebSockets on WordPress VIP are designed to support small-scale, bidirectional, real-time data flows.
Before implementing WebSockets in an application, contact VIP Support and share details about the planned usage and the project requirements.
HTTP request headers
WebSocket client connections require two HTTP request headers to be set in order to set up a handshake with the server:
In many client libraries, these headers are set automatically.
Paths for WebSocket connections must begin with either
Examples of valid paths:
Examples of invalid paths:
WebSocket connections are long-lived but cannot last forever. Normal application lifecycle events such as deploys and autoscaling, as well as network disruptions experienced by users, can close open WebSocket connections without warning.
Because of this, application code should be designed to expect these events and to automatically reestablish WebSocket connections whenever they are disrupted.
Socket.io and long-polling
Socket.io is a popular library for implementing bidirectional communication in Node.js applications. However, Socket.io predates the widespread adoption of WebSockets and does not default to using them.
Instead, Socket.io first establishes a connection via long-polling transport before possibly upgrading to a WebSocket. The long-polling transport is not supported on VIP because it requires consecutive requests to be routed automatically to the same container (sometimes called “sticky sessions”).
To avoid this issue, configure Node.js clients to only use the WebSocket transport.
An example of WebSocket implementation can be found in the Node.js skeleton. The example demonstrates how to integrate a WebSocket server with a separate web server. This allows a traditional website to be served and provides a single WebSocket server instance that is shared by all connections. Creating a new WebSocket server instance for each connection—intentionally or accidentally—can quickly lead to performance problems.
Last updated: December 22, 2023