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

microservice-ws

v1.2.13

Published

[![Version npm](https://img.shields.io/npm/v/microservice-ws.svg?logo=npm)](https://www.npmjs.com/package/microservice-ws)

Downloads

9

Readme

microservice-ws: a Node.js WebSocket library for microservices communications

Version npm

microservice-ws is a simple to use, blazing fast, and thoroughly tested WebSocket client and server, with optional built in authentication.

Table of Contents

Installing

npm install microservice-ws

Usage examples

Sending and receiving text data

import { Client } from 'microservice-ws';

Client({
  host: 'ws://www.host.com',
  port: 8080
}, {
  onConnection: (ws) => {
    ws.send('something')
  },
  onMessage: (data) => {
    console.log('received: %s', data);
  },
  onError: console.error
})

Sending binary data

import { Client } from 'microservice-ws';

Client({
  host: 'ws://www.host.com',
  port: 8080
}, {
  onConnection: (ws) => {
    const array = new Float32Array(5);

    for (var i = 0; i < array.length; ++i) {
      array[i] = i / 2;
    }

    ws.send(array);
  }
})

Simple server

import { Server } from 'microservice-ws';

Server({
  port: 8080
}, {
  onMessage: (data) => {
    console.log('received: %s', data);
  },
  onConnection: (ws) => {
    ws.send('something');
  },
  onError: console.error
})

External HTTP/S server

Coming Soon

Client authentication

import { AuthServer, AuthClient } from 'microservice-ws';

AuthServer({
  port: 8080,
  pingInterval: 60000,
  processAuthToken: (token) => Promise.resolve(token == "valid" ? "test_uid" : null)
}, {
  onAuthenticate: (uid) => {
    console.log('Authenticated: %s', uid);
  },
  onMessage: (data, isBinary, ws) => {
    console.log('Message From: %s', ws.userId);
  },
  onError: console.error
});

AuthClient({
  host: 'ws://www.host.com',
  port: 8080,
  pingInterval: 60000,
  lagBeforeClose: 60000,
  onAuthToken: () => Promise.resolve("your_auth_token")
}, {
  onAuthenticated: (ws) => {
    console.log('Client Authenticated');
  }
});

Server broadcast

A client WebSocket broadcasting to all connected WebSocket clients, including itself.

import { AuthServer } from 'microservice-ws';

AuthServer({
  port: 8080,
  pingInterval: 60000,
  processAuthToken: (token) => Promise.resolve(token == "valid" ? "test_uid" : null)
}, {
  onStart: (wss) => {
    wss.clients.forEach((client) => {
      if (client.readyState === WebSocket.OPEN) {
        client.send("Broadcasted message");
      }
    });
  },
  onError: console.error
});

A client WebSocket broadcasting to every other connected WebSocket clients, excluding itself.

import { AuthServer } from 'microservice-ws';

AuthServer({
  port: 8080,
  pingInterval: 60000,
  processAuthToken: (token) => Promise.resolve(token == "valid" ? "test_uid" : null)
}, {
  onMessage: (data, isBinary, ws, wss) => {
    wss.clients.forEach((client) => {
      if (client !== ws && client.readyState === WebSocket.OPEN) {
        client.send(data, { binary: isBinary });
      }
    });
  },
  onError: console.error
});

Round-trip time

import { AuthClient } from 'microservice-ws';

AuthClient({
  host: 'ws://www.host.com',
  port: 8080,
  pingInterval: 60000,
  lagBeforeClose: 60000,
  onAuthToken: () => Promise.resolve("your_auth_token")
}, {
  onConnection: (ws) => {
    console.log('connected');
    ws.send(Date.now());
  },
  onDisconnect: () => {
    console.log('disconnected');
  },
  onMessage: (data, ws) => {
    console.log(`Round-trip time: ${Date.now() - data} ms`);

    setTimeout(() => {
      ws.send(Date.now());
    }, 500);
  },
  onError: console.error
});

FAQ

How to detect and close broken connections?

Your clients might as well lose connection without knowing it. AuthClient and AuthServer handles this for you to detect and close broken connections.

However Client and Server does not have this functionality to stay bare bones, for efficiency.

License

MIT