Crypto Exchange Websocket Examples Using CCXWS
Tutorials for connecting to every major exchange.
Developing applications that rely on cryptocurrency exchange data is complicated. Every exchange provides a different API which is unique and confusing in its own way.
That’s why we’ve put together a series of tutorials that help developers like you start managing exchanges with ease.
Cryptocurrency traders and developers are constantly looking for the fastest way to access crypto exchange trade and order book data so they can make quicker decisions and better capture opportunities as they happen.
To provide this speed, many developers are turning to websocket APIs. websocket streams provide the latest pricing information as it changes on the exchange. Without requiring developers to constantly poll for new data, exchanges will instantly send the latest trade or order book data whenever there has been a change.
The result is the fastest way to access market data from exchanges. Subscribing to each trading pair is generally a one-time event. After this, the exchange will automatically send updated trade or order book data so you can make real-time decisions on how and when to execute trades.
In this article, we will provide multiple examples for ways you can access live exchange data through websockets for every major crypto exchange.
The examples in this tutorial will cover how to use both the CCXWS JavaScript Library and the Shrimpy Node.js Library.
CCXWS
CCXWS is an open-source library provided by the same developers which brought you CCXT.
Install
Before we can write any code, we need to install the CCXWS package using npm.
npm install ccxws
Example
The following example will connect to two different websocket streams. One trading stream from Binance and one level 2 order book stream from KuCoin.
const ccxws = require("ccxws");const binance = new ccxws.binance();
const kucoin = new ccxws.kucoin();// select binance markets
const subscribeData1 = {
id: "ETHUSDT",
base: "ETH",
quote: "USDT",
};// select kucoin markets
const subscribeData2 = {
id: "LTCBTC",
base: "LTC",
quote: "BTC",
};// handle the trade and order book events from websocket
binance.on("trade", trade => console.log(trade));kucoin.on("l2snapshot", snapshot => console.log(snapshot));// subscribe to the trade and order book events
binance.subscribeTrades(subscribeData1);kucoin.subscribeLevel2Snapshots(subscribeData2);
After connecting to each of these websocket streams, you will immediately begin receiving updates. Each update must be handled using custom logic that will process, organize, and store the data.
Shrimpy
Before beginning with our Node.js example, start by signing up for a Shrimpy Universal Crypto Trading APIs account.
After signing up, select the “Create Api Key” button to generate your Master API Keys. After generating your keys and storing them in a secure location, make sure you have enabled the “Data” permissions on the API keys.
Note: You will need to select a subscription plan before you can begin accessing websocket data. For this tutorial, we recommend subscribing to the “Personal” plan which will provide you with nearly unlimited access to our websockets along with trading, portfolio, and exchange account management endpoints.
npm install shrimpy-node
Example
Similar to our last example with CCXT, this example will subscribe to two different websocket streams. One websocket stream for the Coinbase Pro trade channel and a second subscription for the Kraken order book channel.
import { ShrimpyApiClient, ShrimpyWsClient, ISubscriptionRequest, IWebsocketMessage, IErrorMessage } from 'shrimpy-node';// handle errors from the websockets
let errorHandler = (error: IErrorMessage) => { console.log(error) };// create the Shrimpy API client using Shrimpy master keys
const publicKey = 'your_public_key';
const privateKey = 'your_private_key';
let shrimpyApiClient = new ShrimpyApiClient(publicKey, privateKey);// create the Shrimpy websocket client using a token
let token = shrimpyApiClient.getToken();
let shrimpyWsclient = new ShrimpyWsClient(errorHandler, token);// declare the markets for subscribing and unsubscribing
const subscribeData1: ISubscriptionRequest = {
"type": "subscribe",
"pair": "btc-usd",
"exchange": "coinbasepro",
"channel": "trade"
};const subscribeData2: ISubscriptionRequest = {
"type": "subscribe",
"pair": "eth-btc",
"exchange": "kraken",
"channel": "orderbook"
};const unsubscribeData1: ISubscriptionRequest = {
"type": "unsubscribe",
"pair": "btc-usd",
"exchange": "coinbasepro",
"channel": "trade"
};const unsubscribeData2: ISubscriptionRequest = {
"type": "subscribe",
"pair": "eth-btc",
"exchange": "kraken",
"channel": "orderbook"
};let handler = (msg: IWebsocketMessage) => { console.log(msg); };// connect and subscribe to the websocket channels
client.connect();client.subscribe(subscribeData1, handler);
client.subscribe(subscribeData2, handler);client.unsubscribe(unsubscribeData1);
client.unsubscribe(unsubscribeData2);client.forceDisconnect();
Note: These examples don’t explain how to maintain an order book websocket locally as a client when receiving each update. Additional work will be required to maintain the state of the order books. This will need to be developed based on your requirements for the order book.
Shrimpy supports a Python client in addition to Node.js. You can find that client here.
Conclusion
CCXWS provides a convenient way to subscribe to websockets for each individual exchange. However, CCXWS doesn’t aggregate the websocket streams across exchanges for universal access through the APIs.
Maintaining each websocket can be significant work, so the Shrimpy Websocket APIs are designed to simplify the way you manage all of your exchange websocket connections.
Try out the Shrimpy Trading & Data APIs today. It’s the easiest way to get started collecting data across every major exchange. Collect historical market data, access real-time websockets, execute advanced trading strategies, and manage an unlimited number of users.
Don’t forget to follow us on Twitter and Facebook for updates, and ask any questions to our amazing, active community on Telegram.
The Shrimpy Team