Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@arcblock/ws

Package Overview
Dependencies
Maintainers
4
Versions
303
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@arcblock/ws

OCAP Chain websocket server and client

  • 1.15.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
4
Created
Source

Blocklet Server WebSocket

Blocklet Server PubSub base on Websocket and Phoenix Protocol

Usage

  1. WsServer
const { WsServer } = require('@arcblock/ws');

/**
 * @params {Object} opts
 *  @params {http.Server} opts.httpServer
 *  @params {String} opts.pathname default to '/websocket'
 *  @params {Function} opts.authenticate
 */
const wsServer = new WsServer({
  httpServer: http.createServer(),
  authenticate: (req, cb) => {
    const { searchParams } = new URL(req.url, `http://${req.headers.host || 'unknown'}`);
    const token = searchParams.get('token');
    if (!token) {
      cb(new Error('token not found'), null);
      return;
    }

    // custom logic for validate token
    const authInfo = validateToken(token);

    // if validate success
    cb(null, authInfo);

    // if validate error
    cb(new Error('validate fail'), null);
  },
});

// attach to httpServer(httpServer has passed by constructor)
wsSerer.attach();

// push message
wsServer.push('blocklet.installed', data);
wsServer.push('notification.create', data);
  1. WsClient

WsClient is inherited from Phoenix(source),

import WsClient from '@arcblock/ws/lib/client';

// create instance
const socket = new WsClient(`//${window.location.hostname}`, {
  // params will be passed to server through url
  params: () => ({
    // token is used for authentication
    token: window.localStorage.getItem('abt_node_login_token'),
  }),

  // Defaults to none
  logger: (type, msg, data) => console.log(type, msg, data),
});

// connect
socket.connect();

// add subscriber
socket.on('blocklet.installed', callback1);
socket.on('notification.create', callback2);

// remove subscriber
socket.off('blocklet.installed', callback1);
socket.off('notification.create', callback2);

// disconnect
socket.disconnect(() => {
  // after disconnected...
});
  1. Hooks

It's very simple to create hooks in react apps.

Keywords

FAQs

Package last updated on 22 Mar 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc