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

@soketi/impl

v0.4.4

Published

TypeScript boilerplate to use on your WebSocket implementations. Made with ❤️ at Soketi.

Downloads

135

Readme

The 🇷🇺 Russian invasion of 🇺🇦 Ukraine breaches any law, including the UN Charter. #StandWithUkraine

Open-source is not about political views, but rather humanitar views. It's code by the people for the people. Unprovoked, unjustifiable and despicable action that is killing civilians is not tolerated. The Renoki Co. subsidiaries (including Soketi) has taken action to move away from Russian software and dependencies and block any access from Russia within their projects.

Soketi Implementation

CI codecov Latest Stable Version Total Downloads License

Artifact Hub StandWithUkraine Discord

Soketi Implementation is a TypeScript boilerplate to use on your WebSocket implementations. This represents a customizable single point of entry for your server, no matter what framework you are using.

General Implementations

The package comes with default implementations for the usual WebSocket operations, but as well as specific ones, like Pusher.

This is not providing a WebSocket server, but rather a way to implement your own WebSocket server, no matter what framework you are using.

In the examples, we will assume a pseudo-WebSocket server (not tied to any real use case).

Connections

This implementation provides a tracking of connections for the server:

import { Connections, Connection } from '@soketi/impl';

const conns = new Connections();

server.on('new-connection', async originalConnection => {
    // Generate a unique ID for the connection.
    const uniqueId = Math.random() * 1e5;

    // If possible, associate the unique ID with the original connection.
    // This can be used later to get the connection.
    originalConnection.id = uniqueId;

    // Create a new connection instance, binding
    // the send and close methods to the underlying WebSocket.
    const connection = new Connection(uniqueId, {
        id: uniqueId,
        send: (message) => originalConnection.send(message),
        close: (...args) => originalConnection.close(...args),
    });

    // Add the connection to the connections tracker.
    await conns.newConnection(connection);
});

This way, you can track connections and send messages to them:

for (const conn of conns.connections) {
    // .send will call the send method of the underlying WebSocket.
    await conn.send('Hello!');
}

To undo and remove a connection from the tracker, you can use removeConnection:

await conns.removeConnection(connection);

Handlers for Websocket events

The package provides a way to handle WebSocket events at the general level, so that you don't have to implement them yourself. You will be defining both the handlers, as well as the calls to them, in a static way.

import { Router as WsRouter } from '@soketi/impl';
import { Connections, Connection } from '@soketi/impl';

const conns = new Connections();

WsRouter.onNewConnection(async conn => {
    // As explained earlier in the connections, you can use it to add it to a tracker.
    await conns.newConnection(conn);
    await conn.send('Hello!');
});

server.on('new-connection', async originalConnection => {
    const connection = new Connection(...);

    // Handle the connection via the router.
    // This will call the handler defined above.
    await WsRouter.handleNewConnection(connection);
});

The router provides handlers for the following events:

  • onNewConnection(async (conn, ...args?) => {}) with handleNewConnection(connection, ...args?)
  • onConnectionClosed(async (conn, code, msg, ...args?) => {}) with handleConnectionClosed(connection, code, msg, ...args?)
  • onMessage(async (conn, message, ...args?) => {}) with handleMessage(connection, message, ...args?)
  • onError(async (conn, error, ...args?) => {}) with handleError(connection, error, ...args?)

You can also register your own handlers:

import { Router as WsRouter } from '@soketi/impl';

// Register a ping handler.
WsRouter.registerHandler('onPing', async conn => {
    await conn.send('Pong!');
});

server.on('ping', async originalConnection => {
    // Get the existing connection on a ping or message.
    if (conns.connections.get(originalConnection.id)) {
        await WsRouter.handle('onPing', connection);
    }
});

Pusher

  • Public Channels ✅
  • Presence Channels ✅
  • Private Channels ✅
  • Encrypted Private Channels ✅
  • Client Events ✅
  • Webhooks ❌
  • REST API ❌
  • Metrics ❌

See more: Pusher Channels