# Websocket

## BlofinSocket Object

#### Constructor:

```javascript
 let myBlofinSocket = new BlofinSocket(options);
```

#### Usage:

```javascript
import { BlofinSocket } from "./dev-package/sockets/blofinsocket.js";

  let onOpen = (options) => {
  	console.log("Hello World");
  }

  const exampleOpts = new BlofinSocket({
    key: "myKey",
    secret: "mySecret",
    passphrase: "myPassphrase",
    isPrivate: "true",
    clientOnOpen: onOpen
  });

  let myBlofinSocket = new BlofinSocket(exampleOpts);


```

#### Options:

* apiKey: String
  * Given on creation of API key. Required
* secret: String
  * Secret Key. Required if using private channels
* passphrase: String
  * Passphrased created by user. Required if using private channels
* isPrivate: Boolean
  * Indicates whether to use private channels. False by default
* clientOnOpen: Function(options)
  * Function that takes the options given in the constructor as a parameter and should execute on creation of the websocket once finished opening
* clientOnMessage: Function(options, msg)
  * Function that takes the options given in the constructor and an incoming message as parameters and should execute on reception of said message
* clientOnError: Function(options, msg)
  * Function that takes the options given in the constructor and an incoming error as parameters and should execute on reception of said error
* clientOnClose: Function(code, msg, options)
  * Function that takes a closing code, a message, and the options given in the constructor as parameters and should execute on close

## Login

{% hint style="info" %}
Please refer to the official BloFin API Docs for additional info.

<https://docs.blofin.com/index.html#login>
{% endhint %}

#### Function name:

```javascript
login()
```

#### Usage:

```javascript
let onOpen = (options) => {
  options.socket.login();
}

  let myBlofinSocket = new BlofinSocket({
    key: "myKey",
    secret: "mySecret",
    passphrase: "myPassphrase",
    isPrivate: true,
    clientOnOpen: onOpen
  });

```

## Subscribe

{% hint style="info" %}
Please refer to the official BloFin API Docs for additional info.

<https://docs.blofin.com/index.html#subscribe>
{% endhint %}

#### Function name:

```javascript
subscribe(subscriptions)
```

#### Usage:

```javascript
let onOpen = (options) => {
  options.socket.login();
}


let onMessage = (options, msg) => {
  if (msg.event == 'login'){
    options.socket.subscribe({
                    args:
                        [{ channel: 'account'}]
                });
  }
}

  let myBlofinSocket = new BlofinSocket({
    key: "myKey",
    secret: "mySecret",
    passphrase: "myPassphrase",
    isPrivate: true,
    clientOnOpen: onOpen,
    clientOnMessage: onMessage
  });

```

## Unsubscribe

{% hint style="info" %}
Please refer to the official BloFin API Docs for additional info.

<https://docs.blofin.com/index.html#unsubscribe>
{% endhint %}

#### Function name:

```javascript
unsubscribe(subscriptions)
```

#### Usage:

```javascript
let onOpen = (options) => {
  options.socket.subscribe({

    args:
        [{ 
          channel: 'tickers',
          instId: 'BTC-USDT'
        }]
  });
  }
  
  
  let onMessage = (options, msg) => {
    
    if(msg.event == 'subscribe'){ 
        options.socket.unsubscribe([{ 
          channel: 'tickers',
          instId: 'BTC-USDT'
        }])
    

  }
  

  let myBlofinSocket = new BlofinSocket({
    key: "myKey",
    secret: "mySecret",
    passphrase: "myPassphrase",
    isPrivate: false,
    clientOnOpen: onOpen,
    clientOnMessage: onMessage
  });



```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.compendium.finance/pendax/using-pendax-sdk/blofin-functions/websocket.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
