@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

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
4.0.42latest
4.1.20next

Version History

VersionDownloads (Last 7 Days)Published
4.1.20
4.1.10
4.1.00
4.0.42
4.0.30
4.0.20
4.0.10
4.0.00
4.0.0-rc.90
4.0.0-rc.80
4.0.0-rc.70
4.0.0-rc.60
4.0.0-rc.50
4.0.0-rc.40
4.0.0-rc.30
4.0.0-rc.21
4.0.0-rc.10
4.0.0-rc.00
4.0.0-alpha.61
4.0.0-alpha.50
4.0.0-alpha.40
4.0.0-alpha.30
4.0.0-alpha.20
4.0.0-alpha.10
3.2.10
3.2.00
3.0.10
3.0.00
3.0.0-rc.30
3.0.0-rc.20
3.0.0-rc.10
3.0.0-rc.00
3.0.0-beta.40
3.0.0-beta.30
3.0.0-beta.20
3.0.0-beta.10
3.0.0-alpha.70
3.0.0-alpha.61
3.0.0-alpha.40
3.0.0-alpha.30
3.0.0-alpha.20
3.0.0-alpha.11

Package Sidebar

Install

npm i @pubkeeper/browser-client

Weekly Downloads

6

Version

4.0.4

License

UNLICENSED

Unpacked Size

1.84 MB

Total Files

18

Last publish

Collaborators

  • 32bitkid
  • mattdodge