# Custom Websocket Client



Create Custom Websocket client to connect with CAYC Exchange Server

> For an easier way to use websockets with CAYC Exchange&#x20;

### Requirements

* Nodejs and NPM installed
* Nodejs version >= 12
* Install [Websocket client library](https://www.npmjs.com/package/ws)

### Public Usage

You need to import `ws` library and create a connection with CAYC Exchange, For e.g

```js
const WebSocket = require('ws');
const API_URL = 'https://api.cayc.exchange'

// Public connection
const publicSocket = new WebSocket(`${API_URL}/stream`);
```

### Private Usage

You need to authenticate through the api either using Bearer token or with API key and Secret which you can obtain from [the guide](https://docs.cayc.exchange/docs/api_docs/introduction/#how-to-create-an-api-key)

> API Expires field just used to define the validity of the connection.

```js
const WebSocket = require('ws');
const API_URL = 'https://api.cayc.exchange'

// Bearer Token
const privateSocket = new WebSocket(`${API_URL}/stream?authorization=${BEARER_TOKEN}`);

// HMAC Authentication
const privateSocket = new WebSocket(`${API_URL}/stream?api-key=${API_KEY}&api-signature=${API_SIGNATURE}&api-expires=${API_EXPIRES}`);

// Bearer Token
privateSocket.send(
	JSON.stringify({
		op: 'auth',
		args: [{
			authorization: BEARER_TOKEN
		}]
	})
);

// HMAC Authentication
privateSocket.send(
	JSON.stringify({
		op: 'auth',
		args: [{
			'api-key': API_KEY,
			'api-signature': API_SIGNATURE,
			'api-expires': API_EXPIRES
		}]
	})
);
```

> For a detailed documentation and to learn how it works with CAYC Network, Please visit [the Repository](https://github.com/bitholla/hollaex-kit/tree/documentation/server)
