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

@steijnveer/fbr-plugin-io

v0.1.11

Published

fbr plugin: Socket.io integration

Readme

@steijnveer/fbr-plugin-io

Socket.io integration plugin for @steijnveer/file-based-router

Overview

This plugin adds Socket.io support to your file-based-router application with automatic event handler discovery and registration. Define your Socket.io event handlers in files, and the plugin will automatically load and register them.

Installation

npm install @steijnveer/fbr-plugin-io

Quick Start

1. Configure the plugin

// fbr.config.ts
import ioPlugin from '@steijnveer/fbr-plugin-io';
import defineConfig from '@steijnveer/file-based-router/defineConfig';

export default defineConfig({
  plugins: [ioPlugin],
  io: {
    eventsDir: 'events', // relative to the paths dir, default: 'events'
  },
});

2. Create event handlers

Create event handler files in your events directory (default: src/events):

src/events/message.ts

import type { Socket } from '@steijnveer/fbr-plugin-io';

// Export named functions - function name becomes the event name
export function message(socket: Socket, data: { text: string }) {
  log('Received message:', data.text);
  socket.emit('message:response', { echo: data.text });
}

src/events/chat.ts

import type { Socket } from '@steijnveer/fbr-plugin-io';

export function join(socket: Socket, data: { room: string }) {
  socket.join(data.room);
  socket.to(data.room).emit('user:joined', { id: socket.id });
}

export function leave(socket: Socket, data: { room: string }) {
  socket.leave(data.room);
  socket.to(data.room).emit('user:left', { id: socket.id });
}

src/events/index.ts (for connection handlers)

import type { Socket } from '@steijnveer/fbr-plugin-io';

// Special 'connection' event handlers
export function connection(socket: Socket) {
  log('User connected: ' + socket.id);
  socket.emit('welcome', { message: 'Welcome to the server!' });
}

3. Access Socket.io server instance

The Socket.io server instance is available via the Fbr.server global:

import type { Io } from '@steijnveer/fbr-plugin-io';

// Emit to all connected clients
Fbr.server._io.emit('broadcast', { message: 'Hello everyone!' });

// Access specific rooms
Fbr.server._io.to('room-name').emit('room:message', { text: 'Hello room!' });

Configuration

Configure the plugin via Fbr.Config augmentation in fbr.config.ts:

io?: {
  eventsDir?: string; // Directory for event handler files, relative to paths.srcDir (default: 'events')
}

Event Handler Conventions

Event Naming

  • File name becomes event prefix: message.tsmessage event
  • Named exports: Use function name as event name
    • export function join() in chat.tschat:join event
  • Default exports: Use file name as event name
    • export default function() in message.tsmessage event
  • index.ts: Exports use direct function names
    • export function connection() in index.tsconnection event

Event Handler Signature

type EventData = Record<string, any> | null;
type EventHandler = (socket: Socket, data: EventData) => void;
  • socket: The Socket.io socket instance for the connected client
  • data: The data sent from the client (null if no data provided)

Special Events

  • connection: Handlers named connection are executed when a client connects
  • These handlers run after other event listeners are attached to the socket

Examples

Broadcasting to all clients

src/events/admin.ts

import type { Socket } from '@steijnveer/fbr-plugin-io';

export function announce(socket: Socket, data: { message: string }) {
  // Broadcast to all clients including sender
  socket.server.emit('announcement', { message: data.message });
}

Room-based chat

src/events/room.ts

import type { Socket } from '@steijnveer/fbr-plugin-io';

export function join(socket: Socket, data: { roomId: string }) {
  socket.join(data.roomId);
  socket.to(data.roomId).emit('room:userJoined', { 
    userId: socket.id,
    roomId: data.roomId 
  });
}

export function message(socket: Socket, data: { roomId: string, text: string }) {
  socket.to(data.roomId).emit('room:message', {
    userId: socket.id,
    text: data.text,
    timestamp: Date.now()
  });
}

Event Handler Helpers

The plugin ships a defineEventHandler subpath export with two helpers for writing typed, safe event handlers.

defineEventHandler

A no-op identity wrapper that gives TypeScript the correct inferred type for the handler's data argument:

import { defineEventHandler } from '@steijnveer/fbr-plugin-io/defineEventHandler';

export const message = defineEventHandler<{ text: string }>((socket, data) => {
  // data is typed as { text: string }
  socket.emit('message:response', { echo: data.text });
});

createEventHandler

Wraps a handler with Zod runtime validation. The handler is only called when parsing succeeds; invalid data is silently ignored by default.

import { createEventHandler } from '@steijnveer/fbr-plugin-io/defineEventHandler';
import { z } from 'zod';

export const message = createEventHandler(
  z.object({ text: z.string() }),
  (socket, data) => {
    // data is fully typed as { text: string }
    socket.emit('message:response', { echo: data.text });
  },
);

Provide an optional onInvalid callback to handle validation failures:

export const message = createEventHandler(
  z.object({ text: z.string() }),
  (socket, data) => {
    socket.emit('message:response', { echo: data.text });
  },
  (socket, raw) => {
    socket.emit('error', { message: 'Invalid payload' });
  },
);

Signature:

createEventHandler(schema: ZodType, handler: (socket, data) => void, onInvalid?: (socket, data: unknown) => void)

Client-Side Usage

<!DOCTYPE html>
<html>
<head>
  <script src="/socket.io/socket.io.js"></script>
</head>
<body>
  <script>
    const socket = io();
    
    // Listen for events
    socket.on('welcome', (data) => {
      console.log(data.message);
    });
    
    // Emit events (matches your handler in message.ts)
    socket.emit('message', { text: 'Hello server!' });
    
    // Listen for responses
    socket.on('message:response', (data) => {
      console.log('Echo:', data.echo);
    });
  </script>
</body>
</html>

Debug Logging

The plugin includes built-in debug logging for:

  • User connections/disconnections
  • All incoming events with data
  • All outgoing events with data

Logs include the socket ID for easy debugging.

License

MIT