New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

sse.io-server-nodejs

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

sse.io-server-nodejs

NodeJS server for SSE-IO

  • 1.0.3
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

FAQs

Package last updated on 20 Dec 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc