Websocket
BlofinSocket Object
Constructor:
let myBlofinSocket = new BlofinSocket(options);
Usage:
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
Function name:
login()
Usage:
let onOpen = (options) => {
options.socket.login();
}
let myBlofinSocket = new BlofinSocket({
key: "myKey",
secret: "mySecret",
passphrase: "myPassphrase",
isPrivate: true,
clientOnOpen: onOpen
});
Subscribe
Function name:
subscribe(subscriptions)
Usage:
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
Function name:
unsubscribe(subscriptions)
Usage:
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
});
Last updated