# WebSocket

### Functions

You can connect and subscribe to different WebSocket channels for real-time updates.

To connect, use the `connect` function with the channels you want to subscribe to in an array as the parameter. The connection will reconnect on its own unless you call `disconnect`.

```javascript
client.connect(['orderbook', 'trade']);
```

To disconnect the WebSocket, call `disconnect`.

```javascript
client.disconnect();
```

To subscribe to more channels after connection, use `subscribe`.

```javascript
client.subscribe(['order:1', 'wallet:1']);
```

To unsubscribe from channels after connection, use `unsubscribe`.

```javascript
client.unsubscribe(['orderbook'])
```

### **Channels**

Here is the list of channels you can subscribe to:

* `orderbook`
* `trades`
* `order` (Only available with authentication. Receive order updates for a user of your exchange)
* `wallet` (Only available with authentication. Receive balance updates for a user of your exchange)

For public channels (`orderbook`, `trade`), you can subscribe to specific symbols as follows: `orderbook:xht-usdt`, `trade:xht-usdt`. Not passing a symbol will subscribe to all symbols.

For private channels (`order`, `trade`), you must also pass the user's ID on the HollaEx Network as follows: `order:1`, `wallet:23`. Not passing an ID will give an error.

### **Events**

After connecting to the WebSocket, you can listen for events coming from the server by using the `on` function for the `ws` property of the client. The events available are default WebSocket events e.g. `message`, `open`, `close`, `error`, `unexpected-response`, etc.

```javascript
client.ws.on('message', (data) => {
	data = JSON.parse(data);
	console.log(data);
});
```

These are examples of data responses from the server.

**orderbook**: Updates related to the user's private information are as follows:

```javascript
{
 	"topic": "orderbook",
 	"action": "partial",
 	"symbol": "xht-usdt",
 	"data": {
 		"bids": [
 			[0.1, 0.1],
 			...
 		],
 		"asks": [
 			[1, 1],
 			...
 		],
 		"timestamp": "2020-12-15T06:45:27.766Z"
 	},
 	"time": 1608015328
 }
```

**trade**: Updates related to the user's private information are as follows:

```javascript
 {
 	"topic": "trade",
 	"action": "partial",
 	"symbol": "xht-usdt",
 	"data": [
 		{
 			"size": 0.012,
 			"price": 300,
 			"side": "buy",
 			"timestamp": "2020-12-15T07:25:28.887Z"
 		},
 		...
 	],
 	"time": 1608015328
 }
```

**wallet**: Updates related to the user's private information are as follows:

```javascript
 {
 	"topic": "wallet",
 	"action": "partial",
 	"user_id": 1,
 	"data": {
 		"usdt_balance": 1,
 		"usdt_available": 1,
 		"xht_balance": 1,
 		"xht_available": 1,
 		"xmr_balance": 1,
 		"xmr_available": 1,
 		"btc_balance": 1,
 		"btc_available": 1,
 		"eth_balance": 1,
 		"eth_available": 1,
 		...,
 		"updated_at": "2020-12-15T08:41:24.048Z"
 	},
 	"time": 1608021684
 }
```

**order**: Websocket messages relating to the user's orders.

* The `status` of the order can be `new`, `pfilled`, `filled`, and `canceled`.
* The `action` of the data determines what caused it to happen. All three are explained below:
* `partial`: All previous and current orders. Is the first order data received when connecting. Max: 50. Descending order.

```javascript
{
	"topic": "order",
	"action": "partial",
	"user_id": 1,
	"data": [
		{
			"id": "7d3d9545-b7e6-4e7f-84a0-a39efa4cb173",
			"side": "buy",
			"symbol": "xht-usdt",
			"type": "limit",
			"size": 0.1,
			"filled": 0,
			"price": 1,
			"stop": null,
			"status": "new",
			"fee": 0,
			"fee_coin": "xht",
			"meta": {},
			"fee_structure": {
				"maker": 0.1,
				"taker": 0.1
			},
			"created_at": "2020-11-30T07:45:43.819Z",
			"created_by": 1
		},
		...
	],
	"time": 1608022610
}
```

insert: When user's order is added. The status of the order can be either `new`, `pfilled`, or `filled`.

```javascript
{
	"topic": "order",
	"action": "insert",
	"user_id": 1,
	"symbol": "xht-usdt",
	"data": [
		{
			"id": "7d3d9545-b7e6-4e7f-84a0-a39efa4cb173",
			"side": "buy",
			"symbol": "xht-usdt",
			"type": "limit",
			"size": 0.1,
			"filled": 0,
			"price": 1,
			"stop": null,
			"status": "new",
			"fee": 0,
			"fee_coin": "xht",
			"meta": {},
			"fee_structure": {
				"maker": 0.1,
				"taker": 0.1
			},
			"created_at": "2020-11-30T07:45:43.819Z",
			"updated_at": "2020-12-15T08:56:45.066Z",
			"created_by": 1
		},
		...
	],
	"time": 1608022610
}
```

update: When user's order status is updated. Status can be `pfilled`, `filled`, and `canceled`.

```javascript
{
	"topic": "order",
	"action": "insert",
	"user_id": 1,
	"symbol": "xht-usdt",
	"data": [
		{
			"id": "7d3d9545-b7e6-4e7f-84a0-a39efa4cb173",
			"side": "buy",
			"symbol": "xht-usdt",
			"type": "limit",
			"size": 0.1,
			"filled": 0,
			"price": 1,
			"stop": null,
			"status": "new",
			"fee": 0,
			"fee_coin": "xht",
			"meta": {},
			"fee_structure": {
				"maker": 0.1,
				"taker": 0.1
			},
			"created_at": "2020-11-30T07:45:43.819Z",
			"updated_at": "2020-12-15T08:56:45.066Z",
			"created_by": 1
		},
		...
	],
	"time": 1608022610
}
```
