Subscribing & Unsubscribing
Once connected to the WebSocket API you can subscribe to various feeds using the subscribe
or unsubscribe
commands.
These commands should be JSON with the following properties:
type
string
Command you are sending to the WebSocket (subscribe
or unsubscribe
).
feeds
string[]
List of the feeds that should be subscribed/unsubscribed to/from, respectively.
Subscribing
To begin receiving feed messages, you must send a subscribe
message to the server indicating which feeds to receive. This message is mandatory—you may be disconnected if no subscribe
has been received.
Subscribe Message
// Request -> Subscribe to feed(s)
{
"type": "subscribe",
"feeds": ["<feed 1>", "<feed 2>", ...]
}
Once a subscribe
message is received the server responds with a subscribe
message that lists all feeds you are subscribed to. Subsequent subscribe messages add to the list of subscriptions.
// Response -> Subscribe request
{
"type": "success",
"sequence": 2,
"timestamp": 1727392002151355776,
"payload": {
"action": "subscribe",
"feeds": [
"<feed 1>",
"<feed 2>",
...
]
},
"action": "subscribe"
}
Example Code
const subscribeMsg = {
type: 'subscribe',
feeds: ['ticker.sfox.btcusd']
}
ws.send(JSON.stringify(subscribeMsg));
Unsubscribing
To unsubscribe from a feed or feeds, send an unsubscribe
message. The structure is equivalent to subscribe
messages.
Unsubscribe Message
// Request -> Unsubscribe to feed(s)
{
"type": "unsubscribe",
"feeds": ["<feed 1>", "<feed 2>", ...]
}
Once a unsubscribe
message is received the server responds with an unsubscribe
message that lists all feeds you are unsubscribed from.
// Response -> Subscribe request
{
"type": "success",
"sequence": 4,
"timestamp": 1727394717752870042,
"payload": {
"action": "unsubscribe",
"feeds": [
"<feed 1>",
"<feed 2>",
...
]
},
"action": "unsubscribe"
}
Example Code
const unsubscribeMsg = {
type: 'unsubscribe',
feeds: ['ticker.sfox.btcusd']
}
ws.send(JSON.stringify(unsubscribeMsg))
Last updated