A WebSocket provides a long-lived connection for exchanging messages between client and server. Messages may flow in either direction for full-duplex communication. Commandline access is possible with wscat.

Examples: BlockchainCoinbaseCBIXSlack

How it works: A client creates a WebSocket connection to a server, using a WebSocket client library. WebSocket libraries are generally available in every language, and of course browsers support it natively using the WebSocket JavaScript object. The connection negotiation uses an HTTP-like exchange, and a successful negotiation is indicated with status code 101. After the negotiation response is sent, the connection remains open to be used for exchanging message frames in either binary or unicode string format. Peers may also exchange close frames to perform a clean close.

When to use it: Popular among Bitcoin APIs, WebSockets are great for allowing low-latency tuning of the data to be sent over it (as opposed to HTTP streaming, where a new TCP connection would be needed for each change). Browsers also don’t enforce a connection limit on WebSockets, so they work better for browser apps that use many tabs and communicate with the same domain.

Caveats: While WebSockets as a technology is not exactly new, it is still relatively new in the context of APIs. Also, there is no standard support in WebSocket messages for the things we love about HTTP such as status codes and headers. WebSockets are a great addition to a realtime API, but they should probably not be the sole access mechanism.