@pubkeeper/browser-client
TypeScript icon, indicating that this package has built-in type declarations

4.0.4 • Public • Published

Pubkeeper Browser Client

@pubkeeper/browser-client enables web browsers to join, consume, and produce content on a Pubkeeper network. This package is an aggregate package that combines sane defaults for using Pubkeeper in a browser.

Installation

Using npm:

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

Then, load using ES5 require() syntax…

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

…or with ES2015+ import syntax:

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

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!');
});

Adding a Brewer

client.addBrewer('topic.text', (brewer) => {
  const id = setInterval(() => { brewer.brewText('Hello World!'); });
  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);
  };
});

Related Packages

Readme

Keywords

none

Package Sidebar

Install

npm i @pubkeeper/browser-client

Weekly Downloads

4

Version

4.0.4

License

UNLICENSED

Unpacked Size

1.84 MB

Total Files

18

Last publish

Collaborators

  • 32bitkid
  • mattdodge