Client Examples

Basic examples of work with client

Library

For websockets implementation was used this library

Init Client

import socketClusterClient, { AGClientSocket } from 'socketcluster-client';

const WS_URL = 'ws-pro.kuna.io';
const WS_PORT = 443;

const client = socketClusterClient.create({
    hostname: WS_URL,
    port: WS_PORT,
    secure: true,
});

Authenticate

// JWT Authenticate
void (async () => {
    const accessToken = ""; // your accessToken

    await client.authenticate(accessToken);
    // { isAuthenticated: true, authError: null }
})();

// ApiKey Authenticate by RPC call (socketcluster client)
void (async () => {
  	const apiKey = ""; // your apiKey  
  
    // Invoke a custom 'login' procedure (RPC) on our server socket
  	// then wait for the socket to be authenticated.
  	await Promise.all([
    	socket.invoke('login', apiKey),
    	socket.listener('authenticate').once() // { signedAuthToken: ..., authToken: { ... } }
  	]);
})();

// ApiKey Authenticate by event (any other client)
void (async () => {
  	const apiKey = ""; // your apiKey  
  
    // Send a custom "login" event with your api key
  	// payload: { event: "login", data: apiKey }
  	// then wait for the socket to be authenticated.
  	await Promise.all([
    	socket.transmit('login', apiKey),
    	socket.listener('authenticate').once() // { signedAuthToken: ..., authToken: { ... } }
  	]);
})();

Deauthenticate

// Deauthenticate
void (async () => {
    const res = await client.deauthenticate();
})();

Subscribe

// Subscribe to a channel
void (async () => {
    const arrTickerChannel = client.subscribe('arrTicker');

    await arrTickerChannel.listener('subscribe').once();
})();

Consume data from a channel

// Consume
void (async () => {
    const arrTickerChannel = client.subscribe('arrTicker');

    void (async () => {
        for await (const data of arrTickerChannel) {
          console.log('arrTickerChannel', data);
        }
    })();

    await arrTickerChannel.listener('subscribe').once();
})();

Consume data from auth channel

void (async () => {
    const accessToken = await loginGqlQuery();

    await client.authenticate(accessToken);

    const orderChanel = client.subscribe('order');

    void (async () => {
        for await (const order of orderChanel) {
            console.log('order', order);
        }
    })();

    await orderChanel.listener('subscribe').once();
})();

Full Example

const WS_URL = 'ws-pro.kuna.io';
const WS_PORT = 443;

const client = socketClusterClient.create({
    hostname: WS_URL,
    port: WS_PORT,
    secure: true,
});

void (async () => {
    const accessToken = await loginGqlQuery();

    await client.authenticate(accessToken);

    const tickerChanel = client.subscribe('btc_usdt@ticker');
    const orderChanel = client.subscribe('order');

    for await (const data of tickerChanel) {
        console.log('tickerChanel', data);
    }
    
    for await (const data of orderChanel) {
        console.log('orderChanel', data);
    }
    
    await Promise.all([
        tickerChanel.listener('subscribe').once(),
        orderChanel.listener('subscribe').once(),
    ]);
})();