Web Socket Streams¶
General WSS information¶
- The base endpoint is: https://socket-api.vinex.network/
- The websocket has used socket.io to make streams
- Streams can be accessed either in a single raw stream or in a combined stream
- A single connection to socket-api.vinex.network is only valid for 24 hours; expect to be disconnected at the 24 hour mark
- The websocket client will send a ping frame to server. If the websocket server does not receive a ping frame from the connection within a 10 minute period, the connection will be disconnected.
Install and Setup socket.io client¶
The first of all, install socket.io on client side. It can also be served from a CDN, like cdnjs.
npm install --save socket.io-client
After installed socket.io-client, create a connection to base endpoint
import io from 'socket.io-client'
const socket = io.connect('https://socket-api.vinex.network/', {
'reconnection': true,
'reconnectionDelay': 500,
'reconnectionAttempts': 10,
})
If you use cdnjs, connection be like this
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.3.0/socket.io.js"></script>
<script>
var socket = io('https://socket-api.vinex.network/');
</script>
The structure of a stream¶
<symbol>@<stream>@<interval>
- symbol: All symbols for streams are uppercase and unnderscores, symbol is mandatory.
- stream: Each stream will provide a type of information, stream is mandatory.
- interval: Interval is the latest data update time (millisecond). The default value is 10s.
Example:
BTC_ETH@ticker@10000, BTC_ETH@candleStick@10000, BTC_ETH@orderBook,...
Payload data of streams¶
This part will be used in subscribe and unsubscribe to a stream
candleStick
Payload Property | Mandatory | Data type | Description | Example |
---|---|---|---|---|
interval | required | string | Time interval of the candle | 1m,5m,30m,1h,4h,1d |
startTime | option | timestamp | Start time of the interval | 1538092800 |
endTime | option | timestamp | End time of the interval | 1538092800 |
orderBook: None
ticker: None
marketHistory: None
Live Subscribing/Unsubscribing to streams¶
Subscribe to a stream¶
Create a socket connect and a data object like below. As we can see, socket.emit will send data to server via an event called stream-websocket
socket.on('connect', () => {
const data = {
params: [
{ method: 'SUBSCRIBE', meta: '[email protected]@10000' },
],
}
socket.emit('stream-websocket', data)
})
If you want to subscribe multiple streams, just add the following
params: [
{ method: 'SUBSCRIBE', meta: '[email protected]@10000' },
{ method: 'SUBSCRIBE', meta: '[email protected]@5000' },
{ method: 'SUBSCRIBE', meta: '[email protected]@2000' },
// Add more here
],
We also create an event to receive data from the server and the rules for naming events are as follows structure: symbol_stream. Example: BTC_ETH_marketHistory.
Request¶
socket.on('BTC_ETH_marketHistory', (data) => {
// get data from server
})
Response¶
{
"status":200,
"data":
{
"symbol":"BTC_ETH",
"lastPrice":0.018155,
"highPrice":0.018364,
"lowPrice":0.017953,
"volume":12.20714799,
"quoteVolume":12.20714799,
"baseVolume":671.9368,
"change24h":-0.00520576,
"threshold":0.001,
"bidPrice":0.018081,
"askPrice":0.018181
}
}
Payload additional parameters in the stream¶
In some special cases, the thread will need additional parameters, so we will add the payload attribute to which thread is needed. For example, in the case of thread candles, we need to add parameters like interval, startTime, endTime. We will do the following
params: [
{
method: 'SUBSCRIBE',
meta: '[email protected]@10000',
payload: { interval: '1m', startTime: '1538092800', endTime: '1538092800' }
},
],
Display error information¶
In case of an error, we recommend adding an event called error_info to display the error information. Add code like below
socket.on('error_info', (error) => {
// get error from server
})
Error Message code
Error Message | Description |
---|---|
{ “status”: 1, “msg”: “Invalid request: property name must be a string” } | Property name or meta provided was invalid |
Unsubscribe to a stream¶
To unfollow a thread just update method from SUBSCRIBE to UNSUBSCRIBE and run event stream-websocket again
const data = {
params: [
{ method: 'SUBSCRIBE', meta: '[email protected]@10000' },
{ method: 'UNSUBSCRIBE', meta: '[email protected]@5000' }, // This method will be unsubscribed after change method from SUBSCRIBE to UNSUBSCRIBE
{ method: 'SUBSCRIBE', meta: '[email protected]@2000' },
],
}
socket.emit('stream-websocket', data)