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

socketit

v2.1.1

Published

Socketit: A simple and powerful toolkit for WebSocket communication.

Downloads

116

Readme

Socketit

Socketit: A simple and powerful toolkit for WebSocket communication.

Socketit Logo

Features

  • Supports request-response and publish-subscribe patterns with async handlers.
  • Built-in timeout handling for requests.
  • Customizable routes for handling WebSocket messages.
  • Automatic reconnection for clients.
  • Built-in TLS support with options for self-signed certificates.
  • Compression with permessage-deflate.
  • Ping mechanism to ensure connection health.
  • Works with Node.js.

Installation

npm install socketit

Usage

Server Example

const { Server } = require('socketit');

const server = new Server({
    port: 8888,
    routes: {
        echo: async (data) => data, // Async handler to echo back data
        reverse: async (data) => data.split('').reverse().join(''), // Reverse a string
    },
});

server.on('connection', (channel) => {
    console.log('New client connected');

    channel.on('disconnected', () => {
        console.log('Client disconnected');
    });
});

server.start().then(() => {
    console.log('Server is running on port 8443 with TLS');
});

Client Example

const { Client } = require('socketit');

const client = new Client('wss://localhost:8443', {
    rejectUnauthorized: false, // Allow self-signed certificates
    routes: {
        greeting: async (data) => {
            console.log('Server says:', data);
        },
    },
});

client.on('connected', async (channel) => {
    console.log('Connected to server');

    try {
        const response = await channel.request('echo', { message: 'Hello, server!' });
        console.log('Server responded:', response);

        const reversed = await channel.request('reverse', 'Hello');
        console.log('Reversed string:', reversed);
    } catch (err) {
        console.error('Request failed:', err.message);
    }
});

client.on('disconnected', () => {
    console.log('Disconnected from server');
});

API

Server

Constructor

new Server(options)

Options:

  • port (number): The port for the WebSocket server. Default is 8080.
  • externalServer (http/https server instance): The server to use. If you need TLS, pass an https server
  • routes (object): An object defining methods to handle incoming messages. For example:
    {
        echo: async (data) => data
    }
  • perMessageDeflate (boolean): Enable WebSocket compression. Default is false.

Methods

  • .start(): Starts the WebSocket server. Returns a Promise that resolves when the server is ready.
  • .stop(): Stops the server. Returns a Promise that resolves when the server has stopped.

Client

Constructor

new Client(url, options)

Parameters:

  • url (string): The WebSocket server URL.
  • options (object): Configuration options.
    • autoReconnect (boolean): Automatically reconnect on disconnection. Default is true.
    • rejectUnauthorized (boolean): Allow self-signed certificates. Default is true.
    • perMessageDeflate (boolean): Enable WebSocket compression. Default is false.
    • routes (object): An object defining methods to handle incoming messages.

Methods

  • .close(): Closes the WebSocket connection.

Channel

Methods

  • .request(method, data, options)

    • Sends a request to the server and waits for a response.
    • Supports async handlers.
    • Parameters:
      • method (string): The name of the method to call.
      • data (any): Data to send with the request.
      • options (object): Additional options for the request.
        • timeout (number): Timeout for the request in milliseconds.
  • .publish(method, data)

    • Sends a message to the server without expecting a response.
    • Parameters:
      • method (string): The name of the method to call.
      • data (any): Data to send.
  • .on(event, listener)

    • Listens for events (connected, disconnected, etc.).
    • Parameters:
      • event (string): The event name.
      • listener (function): The callback function.

License

This project is licensed under the MIT License.


Made with ❤️ by Kedem