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

client-streamer

v1.0.6-alpha

Published

A client library used for establishing connection with streaming server to get/push activity stream data and also maintain a persistent queue on client side

Downloads

3

Readme

Client Stream Library

NPM

  • Through this library a program can connect to Streaming Server.
  • It can Send Configuration token, publish activities and can subscribe to activities and events from streaming server.
  • Published activities are sent using a queue mechanism to ensure no data loss.
  • Also one can get all types of activities for a particular application.

API

  • configure(tokenvalue,version,callback)
  • publishSpec(specFileData,token)
  • publishActivity(activity)
  • subscribe(activity,callback)
  • getActivityHistory()

configure(tokenvalue,version,callback)

method used to send jwt token stored in json file to the connected machine defined in the constructor.

syntax :

const Client = require('client-streamer');
const client = new client('127.0.0.1:4000','path/to/sqllite.db');

client.configure('jwt-token','version',(ack)=>{console.log(`ack : ${ack}`)});
client.configure('jwt-token',null,(ack)=>{console.log(`ack : ${ack}`)});

publishSpec(specFileData,token,apiPath)

method used for publishing spec. file(yaml/json) to the apiPath defined as the method parameter using fetch.

syntax :

const Client = require('client-streamer');
const client = new client('127.0.0.1:4000','path/to/sqllite.db');

client.publishSpec(require('./specFile.yaml'),'jwt-token','http://172.0.0.1:8000/register-yaml');

publishActivity(activity)

method used to emit eventType and corresponding activity to the connected machine defined in the constructor.

syntax :

const Client = require('client-streamer');
const client = new client('127.0.0.1:4000','path/to/sqllite.db');

client.publishActivity({'hi':'hello world'});

subscribe(listenForEvent,callback)

method will listen for the event defined at listenForEvent and will perform callback on it.

syntax:

const Client = require('client-streamer');
const client = new client('127.0.0.1:8000','path/to/sqllite.db');

client.subscribe('event',(activity)=>{ ... });

getActivityHistory(applicationName,from,to,activity_code)

provided applicationName and apiPath. User will be able to download token for valid registered application.

if version is not provided, by default, it will download the token for latest version of app.

const Client = require('client-streamer');
const client = new client('127.0.0.1:8000','path/to/sqllite.db');

client.getActivityHistory('AppName','http://path-to-api'); //latest version
client.getActivityHistory('AppName','http://path-to-api','0.0.1'); //for version 0.0.1 (if it exist)

Events

Queue emits events according to the following table:

| Event | Description | Event Handler Parameters | |:-----:|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------| | start | Emitted when the queue starts processing tasks (after calling .startQueue() method) | client.on('start',function(){ }) ; | | next | Emitted when the next task is to be executed. This occurs: * when there are items in the queue and .start() has been called; or * after .add() has been called to add a task to an empty queue and queue isStarted() already | client.on('next',function(job) {  job.id,  job.job }) ; | | add | Emitted when a task has been added to the queue (after calling .addToQueue() method) | client.on('add',function(job) {  job.id,  job.job }) ; | | open | Emitted when the sqlite database has been opened successfully (after calling .openQueuue() method) | client.on('open',function(sqlite) {  sqlite //instance of sqlite3.Database }) ; | | close | Emitted when the sqlite database has been closed successfully (after calling .closeQueue() method) | client.on('close',function() { }) ; |

Contrived Example

This example illustrates the use of the the client library in reading a file and emmiting events.


const { Tail } = require('tail');
const Client = require('client-streamer');
const { ip } = require('./config.js');

const client = new Client(ip, './sql-lite/sqllite.db');

const createActStream = require('./modules/activity-generator-controller');

const tail = new Tail('./logs/production_json.log');

const tokenFile = require('./configure.json');

let disconnectFlag = true;

// Streaming Log File Data
tail.on('line', (data) => {
  const activity = createActStream(JSON.parse(data));

  // console.log(`generated activity :- ${activity}`);

  if (activity !== undefined) {

    // emitted when server gets disconnected
    client.subscribe('disconnect', () => {
      console.log('Disconnected');
      disconnectFlag = true;
    });
    // polls server for connection, emitted when server is connected
    client.subscribe('connect', () => {
      if (disconnectFlag) {
        console.log('connected & sending token again');
        client.configure(tokenFile.token, (ack) => {
          console.log(`acknowledgement for again connect : ${ack}`);
          if (ack !== 'received') {
            disconnectFlag = true;
          }
        }, tokenFile.version);
        disconnectFlag = false;
      }
    });
    //caching of activities
    client.publishActivity(activity);
  }
});
// error handling
tail.on('error', (error) => { console.error(`Error in reading file : ${error}`); });

// sending token at the start of service
console.log('initial sending of token');
client.configure(tokenFile.token,tokenFile.version, (ack) => {
  console.log(`Initial acknowledgement : ${ack}`);
});