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/connections

v0.1.4

Published

`@soketi/connections` is an abstraction package that handles the connections over the WebSockets server.

Downloads

6

Readme

@soketi/connections

@soketi/connections is an abstraction package that handles the connections over the WebSockets server.

Usage

Connections

Connection classes are the main way to interact with the WebSockets server. They are used to send and receive messages.

When a new connection is established, the Connection class has built-in functionalities of a wrapper to handle the underlying WebSocket connection, while offering a simple scaffolding to implement custom logic.

The second parameter is the namespace. It's used to group connections together. For example, you can have a namespace for authenticated users, and another one for guests.

import { Connection } from '@soketi/connections';

const ws = new WebSocket('ws://localhost:8080');

ws.addEventListener('open', () => {
  // The connection is established.
  // Each connection requires a unique ID, which is used for identification.
  // ⚠️ This ID has to be unique in the namespace.
  const connection = new Connection('1', 'default', ws);

  // Listen for messages.
  connection.on('message', (message) => {
    console.log(message);
  });

  // Send a message.
  connection.send('Hello world!');
});

Connections Pool

The ConnectionsPool class is used to manage the connections. It's used to send messages to all connections, or to a specific connection. Here, the namespace parameter is playing a big role.

import { Connection, ConnectionsPool } from '@soketi/connections';

const ws = new WebSocket('ws://localhost:8080');
const pool = new ConnectionsPool();

ws.addEventListener('open', () => {
  const connection = new Connection('1', 'default', ws);

  // Add the connection to the pool.
  pool.newConnection(connection);

  // Send a message to all connections in the "default" namespace.
  pool.broadcastMessage('default', 'Hello world!');

  // Send a message to connection with ID "1" in the "default" namespace.
  pool.send('default', '1', 'Hello world!');
});

Removing connections

To remove a connection from the pool, you can use the removeConnection method:

pool.removeConnection(connection);

Optionally, you can pass a callback to be executed if the removed connection was the last one in the namespace:

pool.removeConnection(connection, () => {
  console.log('No more connections in the namespace.');
});

Close all connections

To close all connections in a namespace, you can use the closeAll method:

pool.closeAll('default', 1000, 'Closing the connection.'); // code and reason are optional

Draining the pool

While closeAll is forcefully closing all connections, the drainConnections method is closing all connections in smaller batches, with a delay between each batch, so that the server is not overloaded and there are no spikes in the reconnection rate in other nodes.

pool.drainConnections(
  // each second, allow a maximum of 10 connections to be closed
  10,

  // reason is optional
  'Closed by the server.',

  // the code is optional
  1000,
);

Development

This library was generated with Nx.

Building

Run nx build connections to build the library.

Running unit tests

Run nx test connections to execute the unit tests via Vitest.