WebSockets

A WebSocket is a persistent bi-directional communication channel between a client (e.g. a browser) and a backend service. In contrast with HTTP request/response connections, websockets can transport any number of protocols and provide server-to-client message delivery without polling.

More on WebSockets

WebSockets are exciting for developers because they allow for bidirectional realtime communication between servers and clients.

WebSockets establish TCP-style connections in a browser-compatible fashion using HTTP during initial setup. Messages over websockets can be provided in any protocol, freeing the application from the sometimes unnecessary overhead of HTTP requests and responses (including headers, cookies, and other artifacts). But most critical is the ability to deliver downstream (server-to-client) messages to connected clients. In the browser, for instance, the same thing was once only possible by polling a server resource, which is a comparatively racy, high-latency, and bandwidth-intensive affair.

WebSockets are available on many platforms, including the most common browsers and mobile devices. They're often applied to solve problems of millisecond-accurate state synchronization and publish-subscribe messaging, both of which leverage Websockets' provision for downstream pushes. A challenge of operating a WebSocket-based system is the maintenance of a stateful gateway on the backend. A WebSocket is erected by making a common HTTP request to that server with an Upgrade header, which the server (after authentating and authorizing the client) should confirm in its response. After this, the connection remains established between that physical client-server pair; if at some point the service needs to be redeployed or the load redistributed, its WebSocket connections needs to be re-established.

How Twilio uses Websockets

At Twilio, we use WebSockets to connect our SDKs to our backend in several of our products:

  • Sync maintains and distributes a single source of state in the cloud, managed by application developers and disseminated to browsers and mobiles. WebSockets provide the downstream (server-to-client) channel necessary to keep the end-user current with the shared state of the application, and provide a very efficient transport of requests from the endpoints to the Twilio backend.
  • Programmable Chat provides an extensible and scalable Chat backend, with purpose-built SDKs that expose chat primitives to developers. WebSockets provide the channel by which Chat disseminates user state, messages, and channel events to the application. They also help detect the availability ("online" or "offline") of a user.
  • TaskRouter automatically routes incoming work—customer phone calls, for example—to agents based on their skills and availability. The realtime state of the task as it is processed, assigned, and completed is pushed to the client over persistent WebSocket connections.