> For the complete documentation index, see [llms.txt](https://docs.compendium.finance/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.compendium.finance/pendax/using-pendax-sdk/blofin-functions/websocket.md).

# 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
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

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

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
