npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dojot/dojot-module

v0.2.3

Published

A library that provides utilities and methods for dojot

Downloads

136

Readme

dojot-module

CodeFactor DeepScan grade codecov npm version

Common library to be used in dojot modules.

Attention: As of version v0.2, this library no longer has integration with the old "auth" service, but with the "keycloak" service. Be aware that the environment variables KEYCLOAK_USER and KEYCLOAK_PASSWORD will probably have to be passed to services that use this library. This user should be able to get the existing realm lists from keycloak.

Overview

This library is intended to handle all the necessary operations that a service needs to perform in order to communicate with other dojot services. These operations are:

  • Sending and receiving messages via Kafka;
  • Subscribing to new Kafka topics whenever a new tenant is created;

Thus, the service should only inform the library of which subjects it is interested in and how the messages should be processed.

How to install

Installing this package is no different than any other from npm. Just execute:


npm install @dojot/dojot-module

which will install this module in its latest version.

How to use

Using it is also very easy. All normal operations involve using the Messenger class, which is responsible for communicating with the message broker (Kafka) and generating events to any component interested in such messages. Also, it provides a very simple mechanism of event generation and subscription so that a library could use it to generate more specific events based on those messages. More on that later.

The folowing code is an example of a code that simply prints any message that it receives from a particular subject.

"use strict";
var dojot = require("@dojot/dojot-module");
var logger = require("@dojot/dojot-module-logger").logger;

var config = dojot.Config;
var messenger = new dojot.Messenger("dojot-snoop", config);
messenger.init();

// Create a channel using a default subject "device-data"
// These "communication channels" will handle all the tenant processing and
// message receival, so that the service implementation would only set the
// message processing callback (done by 'messenger.on' calls).
//
// Creating a new channel will inform the library that the service is interested
// in read/write messages to a particular subject.
messenger.createChannel(config.dojot.subjects.deviceData, "rw");

// Create a channel using a particular subject "service-status"
messenger.createChannel("service-status", "w");

// Register callback to process incoming device data
messenger.on(config.dojot.subjects.deviceData, "message", (tenant, message, extraInfo) => {
  logger.info(`Client: Received message in device data subject.`);
  logger.info(`Client: Tenant is: ${tenant}`);
  logger.info(`Client: Message is: ${message}`);
  logger.info(`Client: ExtraInfo is: ${extraInfo}`);
});

// Publish a message on "service-status" subject using "dojot-management" tenant
messenger.publish("service-status", config.management.tenant, "service X is up");

Configuration

This library might be configured in different ways. Each class (Messenger, Consumer and Producer) receives a configuration object in its constructor. It should have the following attributes at least:

{
  "kafka": {
    "producer": {
      "metadata.brokers.list": "kafka:9092",
      "socket.keepalive.enable": true,
      "dr_cb": true
    },
    "consumer": {
      "group.id": "sample-group",
      "metadata.brokers.list": "kafka:9092",
    }
  },
  "databroker": {
    "url": "http://data-broker",
    "timeoutSleep": 2,
    "connectionRetries": 5,
  },
  "keycloak": {
      "basePath": "http://keycloak:8080/auth",
      "timeoutSleep": 5,
      "connectionRetries": 5,
      "ignoreRealm": "master",
      "credentials": {
        "username": "admin",
        "password": "admin",
        "grantType": "password",
        "clientId":  "admin-cli",
      }
    },
  "deviceManager": {
    "url": "http://device-manager:5000",
    "timeoutSleep": 5,
    "connectionRetries": 3,
  },
  "dojot": {
    "management": {
      "user": "dojot-management",
      "tenant": "dojot-management"
    },
    "subjects": {
      "tenancy": "dojot.tenancy",
      "devices": "dojot.device-manager.device",
      "deviceData": "device-data",
    }
  }
}

All these parameters can be found in config.js file. It is recommended that the service implementation use this file as basis to configure other classes.

The Kafka section follows the parameters used by node-rdkafka library, which in turn follows the configuration of librdkafka. These are the bare minumum that this library needs in order to operate correctly. All the values are the default ones, they might be changed depending on deployment scenario.

The config.js file will also get a few environment variables in order to ease configuration. They are:

export KAFKA_HOSTS = "kafka:9092"
export KAFKA_GROUP_ID = "dojot-module"
export DATA_BROKER_URL = "http://data-broker"
export KEYCLOAK_URL = "http://keycloak:8080/auth"
export KEYCLOAK_USER = "admin"
export KEYCLOAK_PASSWORD = "admin"
export DEVICE_MANAGER_URL = "http://device-manager:5000"
export DOJOT_MANAGEMENT_USER = "dojot-management"
export DOJOT_MANAGEMENT_TENANT = "dojot-management"
export DOJOT_SUBJECT_TENANCY = "dojot.tenancy"
export DOJOT_SUBJECT_DEVICES = "dojot.device-manager.device"
export DOJOT_SUBJECT_DEVICE_DATA = "device-data"

License

The dojot-module-nodejs source code is released under Apache License 2.0.

Check NOTICE and LICENSE files for more information.