# pubcontrol

> EPCP Library

Latest version **1.3.0** (published 2019-06-05) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install pubcontrol
pnpm add pubcontrol
yarn add pubcontrol
bun add pubcontrol
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.3.0 |
| Published | 2019-06-05 |
| First published | 2013-04-05 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 237.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | Fanout, Inc. |
| Maintainers | bokarius, harmony7, jkarneges |
| Keywords | pubcontrol, fanoutpub, fanout, realtime, push, publish |

## Links

- npm: https://www.npmjs.com/package/pubcontrol
- Repository: https://github.com/fanout/node-pubcontrol
- Homepage: https://github.com/fanout/node-pubcontrol#readme
- Issues: https://github.com/fanout/node-pubcontrol/issues
- npm.io page: https://npm.io/package/pubcontrol

## Dependencies (4)

- [jwt-simple](https://npm.io/package/jwt-simple.md) 0.x
- [node-fetch](https://npm.io/package/node-fetch.md) ^2.1.2
- [agentkeepalive](https://npm.io/package/agentkeepalive.md) 2.x
- [eslint-plugin-prettier](https://npm.io/package/eslint-plugin-prettier.md) ^3.0.0

## Alternatives

- [@opentelemetry/exporter-zipkin](https://npm.io/package/@opentelemetry/exporter-zipkin.md) — 14.8M weekly downloads
- [pusher-js](https://npm.io/package/pusher-js.md) — 2.0M weekly downloads
- [browserify](https://npm.io/package/browserify.md) — 1.7M weekly downloads
- [sqs-consumer](https://npm.io/package/sqs-consumer.md) — 1.7M weekly downloads
- [@sanity/eventsource](https://npm.io/package/@sanity/eventsource.md) — 930.8K weekly downloads

## Recent versions

- 1.3.0 (latest) — 2019-06-05
- 1.2.1 — 2018-07-24
- 1.2.0 — 2018-07-17
- 1.1.1 — 2017-09-20
- 1.1.0 — 2015-04-22
- 1.0.7 — 2015-03-24
- 1.0.6 — 2015-02-02
- 1.0.5 — 2015-02-02
- 1.0.4 — 2015-01-23
- 1.0.3 — 2015-01-22
- 1.0.2 — 2015-01-21
- 1.0.1 — 2015-01-21
- 1.0.0 — 2015-01-21
- 0.3.5 — 2014-04-02
- 0.3.4 — 2014-04-02
- … 7 more at https://npm.io/package/pubcontrol/versions

## README

PubControl for NodeJS
======================================

Authors: Katsuyuki Ohmuro <harmony7@pex2.jp>, Konstantin Bokarius <kon@fanout.io>

Description
-----------

EPCP library for NodeJS

HTTP Extensible Pubsub Control Protocol (EPCP) defines a generalized and
extensible data publishing protocol using HTTP. Data is published by way of
HTTP POST, whose content is the JSON string representation of an object that
follows certain structural guidelines.

Each message consists of one or more data items, and is always sent to a
specified channel. The messages are associated with that channel and will
only be delivered to those listeners subscribed to the channel.

This library contains a Format base class and a PubControl class that is used
to send messages. This library also supports the case when the EPCP endpoint
requires certain types of authentication.

Requirements
------------

    jwt-simple

Sample Usage
------------

This example illustrates the process of instantiating the PubControl publisher
class, defining a data format, and then publishing some data.

```javascript
var util = require('util');
var pubcontrol = require('pubcontrol');

var HttpResponseFormat = function(body) { this.body = body; };
util.inherits(HttpResponseFormat, pubcontrol.Format);
HttpResponseFormat.prototype.name = function() { return 'http-response'; };
HttpResponseFormat.prototype.export = function() { return {'body': this.body}; }

var callback = function(success, message, context) {
    if (success) {
        console.log('Publish successful!');
    }
    else {
        console.log('Publish failed!');
        console.log('Message: ' + message);
        console.log('Context: ');
        console.dir(context); 
    }
};

// PubControl can be initialized with or without an endpoint configuration.
// Each endpoint can include optional JWT authentication info.
// Multiple endpoints can be included in a single configuration.

// Initialize PubControl with a single endpoint:
var pub = new pubcontrol.PubControl({
        'uri': 'https://api.fanout.io/realm/<myrealm>',
        'iss': '<myrealm>',
        'key': Buffer.from('<myrealmkey', 'base64')});

// Add new endpoints by applying an endpoint configuration:
pub.applyConfig([{'uri': '<myendpoint_uri_1>'},
        {'uri': '<myendpoint_uri_2>'}]);

// Remove all configured endpoints:
pub.removeAllClients();

// Explicitly add an endpoint as a PubControlClient instance:
var pubclient = new pubcontrol.PubControlClient('<myendpoint_uri>');
// Optionally set JWT auth: pubclient.setAuthJwt(<claim>, '<key>');
// Optionally set basic auth: pubclient.setAuthBasic('<user>', '<password>');
pub.addClient(pubclient);

// Publish across all configured endpoints:
pub.publish('<channel>', new pubcontrol.Item(
        new HttpResponseFormat('Test Publish!')), callback);
```

In some cases, the EPCP endpoint requires authentication before allowing its
use. This library can provide Basic and JWT authentication for these cases. To use Basic authentication instantiate a PubControlClient class, use setBasicAuth() to set the username and password, and add the PubControlClient instance to the PubControl instance via addClient() as shown in the example above. To use JWT authentication pass a configuration to PubControl when instantiating it or via applyConfig and provide the claim as shown in the example above.

If the claim does not contain an exp value, then this library will create an
appropriate value for that field on each use. Since the header is generated
from the authorization object each time it needs to be used, the library is
able to generate a new authorization header, even from the same auth object.

It is also possible to use a literal JWT string for JWT authentication.
This may be useful in certain cases, such as when you are performing a push
request on behalf of another service. That service can preencode the JWT
token and hand it to you in its string representation. This way, that service
does not need to hand the JWT signing key to you.

```javascript
pubclient.setAuthJwt('######.######.######'); // Literal JWT string
```

Running `pubcontrol` in web browsers
-------

`./etc/browser-demo` contains a demo of using webpack to build this library for the web browser, even running it in a separate service worker. The same service worker can be used with Cloudflare Workers as a way of building real-time serverless apps with fanout.io and Cloudflare Workers.

License
-------

(C) 2015 Fanout, Inc.  
Licensed under the MIT License, see file COPYING for details.

---
_Source: https://npm.io/package/pubcontrol · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
