Pubkeeper JavaScript Client

The JavaScript client imports @pubkeeper/browser-client.

Installation

Using npm:

$ npm install --save @pubkeeper/browser-client

and ES5 require() syntax:

var PubkeeperClient = require('@pubkeeper/browser-client').PubkeeperClient;

or ES2015+ import syntax:

import { PubkeeperClient, WebSocketBrew } from '@pubkeeper/browser-client';

Alternatively, you can source it directly from the browser:

<script src="https://unpkg.com/@pubkeeper/browser-client/dist/pubkeeper-browser-client.min.js"></script>

Usage

import { PubkeeperClient, WebSocketBrew } from '@pubkeeper/browser-client';

const client = new PubkeeperClient({
  server: '[ws/wss]://[your-pk-server]:[your-pk-port]/',
  jwt: 'your-pk-token',
  brews: [
    new WebSocketBrew({
      brewerConfig: {
        hostname: 'your-websocket-host',
        port: 'your-websocket-port',
        secure: true,
      },
    }),
  ],
});

client.connect().then(() => {
  console.log('ready!');
});

NOTE: The default encryption of AES-CBC will be used unless otherwise specified for a brewer. See @pubkeeper/client documentation for more details.

Understanding Patron/Brewer Matching

The last argument to the addBrewer() and addPatron() methods are the matched callback. This function will be invoked when Pubkeeper sends a notification that there is at least one Brewer or Patron. One can optionally return an unmatched function that will be invoked when there are no more pairs left; this can be used to cleanup/teardown resources that are needed for this Patron or Brewer.

Adding a Brewer

client.addBrewer('topic.text', (brewer) => {
  const id = setInterval(() => {
    brewer.brewText('Hello World!');
    brewer.brewJSON([{name: 'niolabs', id: 23}]);
  }, 1000);
  return () => { clearInterval(id); };
});

Adding a Patron

client.addPatron('topic.text', (patron) => {
  // is activated
  const handler = (uint8, meta) => {
    console.log('sender:', meta.from);
    console.log('topic:', meta.topic);
    console.log('raw data:', uint8);
    console.log('text data:', new TextDecoder().decode(uint8));
    console.log('JSON data:', JSON.parse(new TextDecoder().decode(uint8)));
  }

  // The message event is always invoked with a Uint8Array as data, no matter which brew*() method was called on the brewing side.
  // Pending a future version, you will need to handle parsing/casting those bytes.
  patron.on('message', handler);

  return () => {
    // deactivation/tear-down
    patron.off('message', handler);
  };
});

[info] Note

The message event is always invoked with a Uint8Array as data, no matter which brew*() method was called on the brewing side. You need to handle parsing/casting those bytes. This will be resolved in future versions of the protocol.

State Change Events

The matched callback will be invoked when there is at least one matching pair; subsequent Brewer/Patron connections will not reactivate the matched function. You can, however, also receive events when any Brewer or Patron is added/removed using the added/removed events:

client.addBrewer('topic.text', (brewer) => {
  const handleAdded = ({ id, topic }) => {
    console.log('patron (%s) added', id);
  };

  const handleRemoved = ({ id }) => {
    console.log('patron (%s) removed', id);
  };

  // setup
  brewer.on('added', handleAdded);
  brewer.on('removed', handleRemoved);

  return function teardown() {
    // cleanup
    brewer.off('added', handleAdded);
    brewer.off('removed', handleRemoved);
  };
});

results matching ""

    No results matching ""