Giter Club home page Giter Club logo

node-sse-pusher's Introduction

SSE-Pusher

npm version Build Status Coverage Status Dependency Status

Simple server-sent events (SSE) for Connect and Express.

Installation

$ npm install --save sse-pusher

API

var ssePusher = require('sse-pusher'); 

var push = ssePusher(); // instantiation variant 1
var push = ssePusher.create(); // instantiation variant 2

push([event,] data)

Pushes an optionally typed (i.e., using the event parameter) event to all connected SSE clients.

Parameters:

  • event - event type, must be a string;
  • data - event data, can be anything that can be serialized using JSON.stringify(). More precisely, anything that is not a string, number or boolean will be serialized using JSON.stringify().

push.handler([mountPath])

Returns a function that can be used both as a Connect/Express middleware and an Express route handler.

Parameters:

  • mountPath - path where the Connect/Express middleware shall be mounted (e.g., /some/path).

Usage

Server-side

First, you have to load the package a instantiate a new SSE-Pusher:

// load package:
var ssePusher = require('sse-pusher'); 

// instantiate a new SSE-Pusher:
var push = ssePusher();

Afterwards, you have to "wire" the SSE-Pusher with you HTTP framework of choice (i.e., Connect or Express):

var app = connect() || express();

// install the pusher as a Connect/Express middleware:
app.use(push.handler('/some/path')); // variant 1
app.use('/some/path', push.handler()); // variant 2

// install the pusher as an Express route handler:
app.get('/some/path', push.handler());

Finally, using push(event, data) or push(data) you can then start pushing data to connected SSE clients:

// push some data:
push('eventname', 'eventdata');

// push some data without specifying an event name:
push({some: 'data'});

Client-side

On the client (i.e., the Web browser) you may then listen to the server-side emitted messages using the following code:

var es = new EventSource('http://localhost:3000/some/path');

// when using push('hello') on the server:
es.onmessage = function (event) {
  console.log(event.data); // logs 'hello'
};

// when using push('greeting', 'world') on the server:
es.addEventListener('greeting', function (event) {
  console.log(event.data); // logs 'world'
});

node-sse-pusher's People

Contributors

pgaubatz avatar

Stargazers

Roman avatar

Watchers

Anton Kulakov avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.