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

sse.io-server-nodejs

v1.0.3

Published

NodeJS server for SSE-IO

Downloads

1

Readme

SSE-IO

NodeJS server for SSE-IO.

How to use

const sseio = require('sse.io-server-nodejs')

Import using ES6

import * as sseio from 'sse.io-server-nodejs';

The following example attaches sse.io to a plain Node.JS HTTP server listening on port 3000.

const server = http.createServer();
const sseServer = sseio.newServer(server, { path: '/user/:userId/foo' });

const eventHandler = sseServer.registerEventHandler('event', {
  getRoomId: (context): string => {
    return context.params.guid;
  },
});

server.listen(3000);

eventHandler.send('roomId', 'message');

Standalone

const sseServer = sseio.newServer({ path: '/user/:userId/foo' });

// ... regist event handler

sseServer.listen(3000);

In conjunction with Koa

const app = new require('koa')();
const server = require('http').createServer(app.callback());

const sseServer = sseio.newServer(server, {
  path: '/files/:fileGuid/pull'
})

// ... regist event handler

server.listen(3000);

In conjunction with Express

const app = require('express')();
const server = require('http').createServer(app);

const sseServer = sseio.newServer(server, {
  path: '/files/:fileGuid/pull'
})

// ... regist event handler

server.listen(3000);

API

SSEIO

newServer(httpServer, options)

  • httpServer http.Server The server to bind to.
  • options (Object)
    • path (String) The url path. You can add path params as well like /users/:userId.
  • Returns Server

newServer(options)

  • options (Object) See above for available options.
  • Returns Server

Usually using for standalone mode.

Server

server.registerEventHandler(event, options)

  • event (String) The event you want to handle.
  • options (Object)
    • getRoomId (Function) (context: sseContext) => string Return the room ID.
    • fetch (Function, Optional) (context: sseContext) => Promise<any> It will be executed once after a client is connected, and send the result to the client
  • Returns EventHandler

server.listen(port)

Starts the HTTP server listening for connections, usually using for standalone mode.

server.on(event, callback)

Register a handler for the event. The 'error' event must be handled.

Event: 'conn:create'

  • callback (Function) The clientId will be passed to the callback function

Fired when a new connection is established.

sseServer.on('conn:create', clientId => {
  console.log(clientId);
})

Event: 'conn:close'

  • callback (Function) The clientId will be passed to the callback function

Fired when a connection is closed.

Event: 'error'

  • callback (Function) an Error will be passed to the callback function

Fired when an error occurs.

sseServer.on('error', err => {
  console.error(err);
})

Type: sseContext

It's an object containing params and query keys.

  • params The path params parse from url
  • query The query params parse from url

EventHandler

eventHandler.send(roomId, message)

  • roomId (String) Room id
  • message (any) Message be to send. It can be any type. If it's not a string, then it will be stringify by JSON.stringify().

Send a message to the clients in room.

eventHandler.on(event, callback)

Register a handler for the event.

Event: 'room:create'

  • callback (Function) The roomId will be passed to the callback function

Fired when a room is created.

eventHandler.on('room:create', roomId => {
  console.log('a room:', roomId, 'has been created');
})

Event: 'room:empty'

  • callback (Function) The roomId will be passed to the callback function

Fired when a room is empty.