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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@progalaxyelabs/stonescriptphp-chat

v0.1.1

Published

Generic Socket.IO chat server with JWKS authentication and plugin hooks for StoneScriptPHP platforms

Readme

@progalaxyelabs/stonescriptphp-chat

Generic Socket.IO chat server with JWKS authentication and plugin hooks. Designed to plug into any StoneScriptPHP platform (or any Node.js service) with zero hardcoded tenant assumptions.

Features

  • Socket.IO (WebSocket) — real-time bidirectional chat
  • JWKS authentication — RS256 / ES256 only; alg=none and HS256 rejected before key lookup
  • Plugin hooksresolveRoom, persistMessage, onUserJoin, etc. keep business logic out of the library
  • In-memory room registry — swappable for Redis via hooks for multi-node deployments
  • Delivery confirmations — ack callbacks on join and message events
  • Presence eventsjoined / left broadcast on connect/disconnect

Install

npm install @progalaxyelabs/stonescriptphp-chat

Quick Start

import http from 'node:http';
import { createChatServer } from '@progalaxyelabs/stonescriptphp-chat';

const httpServer = http.createServer();

const chatServer = createChatServer({
  httpServer,
  jwks: {
    url:      process.env.JWKS_URL,      // e.g. https://auth.example.com/.well-known/jwks.json
    issuer:   process.env.JWT_ISSUER,
    audience: process.env.JWT_AUDIENCE,
  },
  hooks: {
    // REQUIRED — scope rooms by tenant so tenants never bleed into each other
    resolveRoom: async (user, roomRequest) => `${user.tenant_id}:${roomRequest}`,

    // Optional — persist messages to your database
    persistMessage: async (msg) => {
      await db.query('INSERT INTO messages ...', [msg.id, msg.roomId, msg.from, msg.text]);
    },
  },
});

httpServer.listen(process.env.PORT ?? 3000);

Environment Variables

| Variable | Required | Description | |-----------------|----------|------------------------------------------------| | JWKS_URL | Yes | Remote JWKS endpoint URL | | JWT_ISSUER | Yes | Expected iss claim in JWT | | JWT_AUDIENCE | Yes | Expected aud claim in JWT | | REDIS_URL | No | For multi-node deployments (via custom hooks) | | PORT | No | HTTP server port (default: 3000) | | CORS_ORIGINS | No | Comma-separated allowed origins |

Plugin Hooks

All hooks are async functions. Provide them in the hooks option to createChatServer.

authenticateConnection(socket, token)userPayload

Verify the JWT and return the user payload. Default delegates to the JWKS verifier.

resolveRoom(user, roomRequest)roomId (REQUIRED)

Map a user and room request to a concrete room ID. Use this to scope rooms by tenant:

resolveRoom: async (user, roomRequest) => `tenant:${user.tenant_id}:${roomRequest}`

authorizeRoom(user, roomId)boolean

Return false to deny access. Default: allow all authenticated users.

persistMessage(msg)void

Store the message to your database. Called after delivery (fire-and-forget — errors don't affect delivery).

onMessageDelivered(msg, recipients)void

Called after a message is sent, with the list of recipient socket IDs.

onUserJoin(user, roomId)void

Called when a user successfully joins a room.

onUserLeave(user, roomId)void

Called when a user leaves a room (disconnect or explicit leave).

Client Events (Socket.IO)

Client → Server

| Event | Payload | Ack | |-----------|-------------------------------------------|------------------| | join | { room: roomRequest } | (err, roomId) | | leave | { room: roomId } | — | | message | { room: roomId, text: string } | (err, msg) | | typing | { room: roomId, isTyping: boolean } | — |

Server → Client

| Event | Payload | |------------|--------------------------------------------------------------| | message | { id, roomId, from, text, timestamp } | | typing | { roomId, from, isTyping } | | presence | { roomId, userId, event: 'joined' \| 'left' } | | error | { message } |

Authentication (Client Side)

Pass the JWT in the auth option when connecting:

import { io } from 'socket.io-client';

const socket = io('wss://chat.example.com', {
  auth: { token: yourJwtToken },
  transports: ['websocket'],
});

socket.on('connect_error', (err) => {
  if (err.data === 'JWTExpired') {
    // Refresh token and reconnect
  }
});

Server-Side Push

Publish a message to a room from your application code (e.g. triggered by a webhook from the PHP API):

chatServer.publish('tenant:abc:support', {
  id: crypto.randomUUID(),
  roomId: 'tenant:abc:support',
  from: 'system',
  text: 'Your order has been shipped.',
  timestamp: new Date().toISOString(),
});

Multi-Node Deployments (Redis)

For horizontal scaling, override the room hooks to use Redis Pub/Sub:

import { createAdapter } from '@socket.io/redis-adapter';
import { createClient } from 'redis';

const pubClient = createClient({ url: process.env.REDIS_URL });
const subClient = pubClient.duplicate();
await Promise.all([pubClient.connect(), subClient.connect()]);

const chatServer = createChatServer({ httpServer, jwks, hooks });
chatServer.io.adapter(createAdapter(pubClient, subClient));

License

MIT