0%
Overview page background
HomeOverviewsWeb3 Tools
What Is a WebSocket and How Does It Work

What Is a WebSocket and How Does It Work

Alchemy headshot

Written by Alchemy

Brady Werkheiser headshot

Reviewed by Brady Werkheiser

Published on June 20, 20224 min read

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. 

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.  

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.

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.

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. 

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.

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. 

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.

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.

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

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.

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.

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:

  1. Tracking pending and mined Ethereum transactions 

  2. Building a dApp with real-time transaction notifications

  3. Integrating Alchemy webhooks with Zapier

Alchemy Notify can even be used to send email notifications to your dApp users, preventing frustration on the status of their transactions.

Overview cards background graphic
Desktop section background image

Build blockchain magic

Alchemy combines the most powerful web3 developer products and tools with resources, community and legendary support.

Get your API key