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

socket-lemur

v1.3.0

Published

The socket-lemur library simplifies WebSocket connections for real-time communication in chat applications and online games. The SocketServer class integrates JWT authentication and API key validation. The SocketClient class facilitates server communicati

Downloads

250

Readme

SocketServer

SocketServer is a utility class for managing WebSocket channels using Socket.IO, with integrated JWT authentication and API key validation.

Installation

Ensure you have Node.js and npm installed. Then install the required packages:

`npm install socket-lemur`

Usage

Initialize the SocketServer instance and define custom event handlers for your application.

const { SocketServer } = require("socket-lemur");

// Port Definition
const PORT = process.env.PORT || 4000;

// SocketServer Definition
const server = new SocketServer<{ id: string }>({
  apikey: "api-key",
  secret: "jwt-secret",
  roomsEnabled: true,
});

const data = [
  { id: 1, name: "Pizza" },
  { id: 2, name: "Pasta" },
];

// Actions Definition
async function get() {
  return data;
}

async function add(product: { name: string }) {
  data.push({ id: data.length + 1, name: product.name });
  return data;
}

// Channels Definition
server.channel<any[]>("get/products", async function (_, res) {
  const products = await get();
  res(products);
});

server.channel<{ name: string }>(
  "post/products",
  async function (req, res) {
    const products = await add(req.body);
    res(products);
  },
  true
);

// Server run on port
server.listen(PORT, function () {
  console.log(`Server Run http://localhost:${PORT}`);
});

// On connection
server.connection({
  on: function () {
    console.log("onConnection");
  },
  off: function () {
    console.log("onDisconnect");
  },
});

// More event definitions can be added here

Constructor

new SocketServer<S>(settings?)

Creates a new instance of SocketServer.

const default: Partial<ServerOptions> = {
    cors: {
        origin: "*", // Configure CORS as needed
        methods: ["GET", "POST"],
        credentials: true,
    },
    allowEIO3: true
};
  • apikey (optional): API key for validating requests.
  • secret (optional): Secret key for JWT token validation.
  • options (optional): Socket.IO server settings default.
  • roomsEnabled (optional): Whether to enable room support false.

Methods

channel(name, onEvent, tokenRequire, roomSupport)

Establishes channel handling and defines event listeners for Socket.IO.

Parameters:

Initialize handling for a channel with optional room support.

  • name: {string} - The name of the channel.
  • onEvent: {onEvent} - Callback to handle incoming events.
  • tokenRequire: {boolean} - Whether token authentication is required for events on this channel false.
  • roomSupport: {boolean} - Whether room support is enabled for this channel this.roomsEnabled.

listen:

This method receives the same parameters or configuration from an http server.

  • port: {number}.
  • hostname: {string}.
  • backlog: {number}.
  • listeningListener: {function}.

connection:

The server's connection method receives an object with two properties on and off, on detects when a client connects and off when that client disconnects.

  • on: {function} connection callback function.
  • off: {function} disconnection callback function.

SocketClient

SocketClient class facilitates WebSocket communication with a server using Socket.IO.

Usage

Initialize SocketClient to connect to a WebSocket server and handle events.

const { SocketClient } = require("socket-lemur");

const PORT = 3030;
const url = `http://localhost:${PORT}`;

// Initialize SocketClient with api_key
const socket = new SocketClient(url, {
  apiKey: "api-key",
});
// Initialize SocketClient with api_key and token
const socket = new SocketClient(url, {
  apiKey: "api-key",
  token: "token",
});

function error(error) {
  console.error("Event error:", error);
}
function success(data) {
  console.error("Event success:", data);
}
// Connect to a WebSocket channel and define event handlers
const postProduct = socket.channel<any>("post/products", {
  onSuccess: success,
  onError: error,
  room: "post",
});

const geProducts = socket.channel<any[]>("get/products", {
  onSuccess: success,
  onError: error,
});

geProducts.on(); // Adds the listener function to the end of the listeners array for the event named eventName.
geProducts.off(); // Removes the specified listener from the listener array for the event named eventName.
geProducts.emit();
postProduct.emit(
  {
    data: { name: "coffe" },
    params: { room: "post" },
  },
  "tokent"
);

Constructor

new SocketClient(serverUrl, apiKey, authToken?)

Creates an instance of SocketClient to connect to a WebSocket server.

interface Security {
  apiKey?: string;
  token?: string;
}
  • serverUrl: {string} - The URL of the WebSocket server.
  • security: {Security} - Optional security options {apiKey, token}.
  • onError: {OnErrorCallback} - Optional callback to handle errors.

Methods

channel(name, opts: LemurOpts): EmitEvent

Connects to a WebSocket channel and sets up callbacks for error and success events.

declare type LemurOpts<T> = {
  onSuccess: OnSuccessCallback<T>;
  onError?: OnErrorCallback;
  room?: string;
};
  • name: {string} - The name of the channel to connect to.
  • opts: {LemurOpts} - The options for the channel, including success and error callbacks, and an optional room.

The channel method returns an object with multiple actions

  • emit: {(data?, token?) => void} - Emits an event to the channel with optional data and token.
  • off: {() =>void} - Removes the event listeners for the channel.
  • on: {() =>void} - Event listeners for the channel.

Annotations

To implement SocketClient in vite you must configure this plugin to use Buffer.

  • npm i -D vite-plugin-node-polyfills
import { defineConfig } from "vite";
import { nodePolyfills } from "vite-plugin-node-polyfills";

export default defineConfig({
  plugins: [
    // ... other plugin
    nodePolyfills(),
  ],
});