What Is a WebSocket and How Does It Work
Written by Alchemy
Reviewed by Brady Werkheiser
WebSocket is a bidirectional communication protocol based on TCP that standardizes communication between a client and a server, allowing for both parties to request data from one another. In contrast, a unidirectional protocol like HTTP only allows for the client to request data from the server.
A WebSocket connection between a client and a server can stay open as long as the parties wish it to maintain the connection, allowing for continuous communication.
With HTTP, each connection begins when the client makes a request and ends the connection when the request is fulfilled.
WebSockets may be superior for Web3 dApp notifications because they enable real-time notifications for critical events continuously compared to requiring individual requests. Tools like Alchemy Notify make it simple to add real-time notifications to wallets and dApps.
What is WebSocket?
WebSocket is a bidirectional communication protocol that enables interactive communication sessions between a client and server. It’s based on TCP and is often used for apps and services that want real-time notification capabilities.
What is a WebSocket server?
A WebSocket server is an application that is listening on a TCP port, following a specific protocol. WebSocket is a bidirectional communications protocol between a client and a server, allowing for both of them to request and send data to one another.
In contrast, HTTP is a unidirectional communications protocol, where the client can only send requests to the server and the server can only send data back in response—never can the server in a HTTP relationship request from the client.
What is a WebSocket connection?
A WebSocket connection is a continuous connection between the client and server. In contrast, HTTP connections are one-time only. The connection begins with every request the client makes from the server and ends with the server’s response. WebSocket connections can be maintained for as long as the client and servers wish it to be open, meaning data can flow through that WebSocket as long as the parties wish—all from one initial request.
What protocol does WebSocket use?
WebSocket uses the WS protocol, which is based on the Transmission Control Protocol (TCP). It is a connection-oriented network, meaning a connection between participants has to be established first to route data through to the right location.
In contrast, the Internet Protocol determines where data is sent based on information within that packet of data; no pre-setup is required to route the packet.
What is a WebSocket API?
There are two ways for a server to send data to a client. The client can regularly request data from the server, known as polling, or the server can automatically send data to the client, known as server push.
WebSocket APIs take advantage of the connection between the client and server by staying open after the initial request to use the server pushing technique, removing the infrastructure stress created from clients constantly polling the server for new updates.
How do WebSockets work?
WebSockets are a bidirectional communication method, allowing for multiple responses from a single server request. WebSockets are also mostly used for client-server communication, while webhooks are mostly used for server-server communication.
How do WebSockets work differently compared to webhooks?
In contrast to WebSockets, webhooks, which use HTTP, are strictly unidirectional: the server only responds to applications when a request is made, and every time a request is fulfilled, the connection is terminated.
Alchemy Notify uses webhooks to communicate between servers and WebSockets to push the notifications that the dApp user sees.
What are web3 WebSockets used for?
Alchemy’s Subscription API provides two new API methods that extend the functionality of the original Ethereum API - eth_subscribe and eth_unsubscribe - that enable Web3 developers to use WebSockets for continually receiving information or data such as getting pending transaction information, pending transaction hashes, new header blocks, and logs.
Here are the supported methods of Alchemy’s Subscription API that uses WebSockets:
alchemy_newFullPendingTransactions - returns the full transaction information (not just transaction hashes) for all transactions that are added to the pending state
alchemy_filteredNewFullPendingTransactions - returns the transaction information for all pending transactions that match a to or from address filter
newPendingTransactions - returns the hash for all transactions that are added to the pending state regardless if you sent them or not
newHeads - sends an event each time a new header (block) is added to the blockchain
logs - sends logs that are part of newly added blocks that match specified filter criteria
When any existing WebSockets connection needs to be terminated, the eth_unsubscribe call can be used along with the subscription ID parameter.
When to use WebSockets vs Webhooks
The tradeoff between using WebSockets or webhooks comes from whether your infrastructure design can better handle many open WebSocket connections concurrently versus many webhook connection requests from clients.
If your server application runs as a cloud function (AWS Lambda, Google Cloud Functions, etc) use webhooks because the application will not keep the WebSocket connections open.
If the quantity of notifications being sent is low, webhooks are also superior in that the connections are begun only on the condition of an event happening.
If the event is rare, it is better to use webhooks than to keep open many WebSocket connections between the client and server.
Lastly, whether one is trying to bridge a server with another server or a client and a server matters too; webhooks are better for the former, WebSockets for the latter. We’ve created a Github repo for setting up a webhook server.
When to use the WebSocket Protocol
For many Web3 dApps, it’s mandatory to update your users in real-time on the status of their transactions. Otherwise, they may have a poor user experience and abandon your app or service. Alchemy Notify solves this by letting users know the exact status of their transactions.
When to use WebSockets over HTTP
WebSockets should be used over HTTP requests whenever latency needs to be the lowest amount possible so users receive notifications on events as fast as they happen. HTTP is comparatively much slower because the client is limited in how often it can receive updates by how often it sends out requests.
WebSocket Examples
We’ve created guides on how to implement Alchemy Notify to send in-app notifications updating your users for critical events like when their wallet balance changes. Here are a few tutorials we've published:
Alchemy Notify can even be used to send email notifications to your dApp users, preventing frustration on the status of their transactions.
Related overviews
Learn About the Challenges and Solutions Engineers Face for Monitoring Their Dapp's Health
This guide shares best practices on how to best prepare for launching your dapp
Learn How Onchain Data Analysis Tools Can Curate User Information and Make Suggestions to Improve Engagement