npm

strong-pubsub-nats-streaming

1.0.5 • Public • Published

strong-pubsub-nats-streaming

NATS Adapter for strong-pubsub

Installation

$ npm install strong-pubsub-nats-streaming

Use

module.exports = async function(app) {
  var Client = require("strong-pubsub");
  var Adapter = require("loopback-pubsub-nats");

  var client = new Client({ cluster: "test-cluster", client: "test", server: "nats://localhost:4222" }, Adapter);

  client.connect();

  client.on("error", err => {
    console.log(err);
  });

  client.on("connect", () => {
    console.log("connected");

    var opts = client.adapter.subscriptionOptions();
    client.subscribe("test", (err, subClient) => {
      console.log("subscribed");
      subClient.on("message", msg => {
        console.log(
          "Received a message [" + msg.getSequence() + "] " + msg.getData()
        );
      });

      setTimeout(() => {
        client.end();
      }, 5000);

      setInterval(() => {
        client.publish("test", "test", (err, resp) => {
          if (err) console.log(err);
          console.log("published: ", resp);
        });
      }, 1000);
    });
  });
};

Environment variables

Setting up nats environment variables

Nats requires the following env variables to be set

NATS_CLUSTER=default
NATS_CLIENT=default
NATS_SERVER=nats://localhost:4222

Package Sidebar

Install

npm i strong-pubsub-nats-streaming

Weekly Downloads

0

Version

1.0.5

License

MIT

Unpacked Size

12.8 kB

Total Files

5

Last publish

Collaborators

  • uzarsalan