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

@allbin/express-notifications

v0.0.8

Published

Websocket notifications for API entities

Downloads

18

Readme

@allbin/express-notifications

import Notifications from '@allbin/express-notifications';
import express from 'express';

const app = express();
const notifications = new Notifications(app, {
    path: '/message-updates',
    jwtSecret: 'shhhh',
    jwtVerificationOptions: {
        audience: 'https://example.com'
    }
});

app.use(...);

let messages = [];

notifications.on('client_authenticated', (ws) => {
    ws.send({type: "messages:existing", data: messages});
});

app.get('/messages', (req, res, next) => {
    res.status(200).json(messages));
});


app.post('/messages', (req, res, next) => {
    const msg = JSON.parse(req.body);
    messages.push(msg);
    notifications.push('messages:created', [msg]);
    res.status(201);
});

app.listen(process.env.PORT);

Authentication

If options.jwtSecret was set during initialization, clients are expected to present credentials before receiving any notifications. This is done by sending an auth message containing a JWT.

{
    "type": "auth",
    "data": {
        "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c"
    }
}

If the token is accepted by the server, the client will start receiving notification pushes. Note that the client connection will be terminated if its associated token expires. To avoid service interruption, the client is therefore expected to periodically acquire new tokens and issue additional "auth" messages as needed.

Heartbeats

Connected clients are expected to respond to periodical pings from the server. These will arrive in the following form:

{
    "type": "ping"
}

And should simply be responded to with:

{
    "type": "pong"
}

Functions

Notifications(app, [http_server], [options])

Creates a websocket server with client connection management.

Returns a Notifications object.

Arguments: app - An express app
http_server - An optional instance of a node http.Server. if not supplied, one will be created for you.
options - See below.

Accepted options: path - Route path for the WebSocket server to use. default: /notifications
pingInterval - Seconds between WebSocket pings sent to clients, default: 30
jwtSecret - Passed unchanged to jsonwebtoken.verify. default: undefined
jwtVerificationOptions - Passed unchanged to jsonwebtoken.verify. default: undefined

notificationInstance.push(type, data, [targeted_claims])

Pushes a notification message to all registered clients in the form:

{
    "type": type,
    "data": data
}

Available options: type - a string meant to communicate to clients what type of notification this is.
data - type-dependent data.
targeted_claims - notification will only be sent to authenticated users who possess the specified claims.

Events

client_authenticated(ws)

Emitted when a client has successfully authenticated for the first time. Subsequent authentication messages will not trigger a new emission of this event. It is emitted exactly once per client connection.

If options.jwtSecret was not set, this event is emitted immediately upon connection.

If options.jwtSecret was set, ws.user contains the decoded JWT payload.