How to use websockets when building on Solana.
What are WebSockets and how do they differ from HTTP requests?
WebSockets is a bidirectional communication protocol that maintains a network connection between two parties, typically a server and a client. Unlike HTTP, with WebSockets clients don't need to make requests when they want information continuously.
Instead, in an open WebSocket connection, a server can push network updates to clients by allowing them to subscribe to certain network states, such as new slots from the blockchain.
How can I set up a WebSocket connection?
It’s quite simple to set up a new WebSocket connection to Solana - try the command below in your terminal.
wscat -c wss://api.zan.top/node/ws/v1/solana/devnet/{APIKEY}
wscat -c wss://api.zan.top/node/ws/v1/solana/mainnet/{APIKEY}
If you’d like an endpoint with higher rate limits in Solana, please contact us.
After connecting to the RPC PubSub WebSocket at wss://<ADDRESS>/
:
- Submit subscription requests to the WebSocket using the methods below
- Multiple subscriptions may be active at once
How can I make WebSocket subscriptions for Solana blockchain reliable?
Once you’ve run the command above, you’ll have an open WebSocket connection to a Solana node. To start/stop receiving push updates on certain state changes in the Solana network, you may need to send one of the following methods:
- **Subscribe
- **Unsubscribe
These two requests enable blockchain app developers to create and delete subscriptions. By setting their parameters properly, you’ll get push updates whenever new slots are sent or new blocks are created.
However, please note that due to the high block production speed and high traffic of Solana nodes, the WebSocket connection may occasionally disconnect during node maintenance. Users need to actively manage exceptions and resubscribe when the connection is lost.
Note:
If you meet any problems when using Solana Websocket, please contact us.