sse.io-server-nodejs
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

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)

  • port (Number) the port to listen on
  • Returns http.Server

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.

Readme

Keywords

Package Sidebar

Install

npm i sse.io-server-nodejs

Weekly Downloads

1

Version

1.0.3

License

ISC

Unpacked Size

55 kB

Total Files

16

Last publish

Collaborators

  • neoooooye