Lightweight long polling module for express.js
Description
Sets up basic long poll with subscribe and publish functionality.
Install
$ npm install -S express-longpoll
Usage
Basic initalization
var express = ;var app = ;var longpoll = app// You can also enable debug flag for debug messagesvar longpollWithDebug = app DEBUG: true ;
Quick-start code
Server - server.js
var express = ;var app = ;var longpoll = app; // Creates app.get("/poll") for the long polllongpoll; app; var data = text: "Some data" ; // Publishes data to all clients long polling /poll endpoint// You need to call this AFTER you make a GET request to /polllongpoll; // Publish every 5 seconds;
Client - index.js (with jQuery)
var { $;}; // Make sure to call it once first,;
longpoll.create(url, [options])
Sets up an express endpoint using the URL provided.
var longpoll = app; longpoll;longpoll; // set max listeners
longpoll.create(url, middleware, [options])
Set up an express endpoint using the URL provided, and use middleware.
var longpoll = app; longpoll;
longpoll.publish(url, data)
Publishes data
to all listeners on the url
provided.
var express = ;var app = ;var longpoll = app; longpoll; longpoll;longpoll;longpoll;
longpoll.publishToId(url, id, data)
Publish data to a specific request. See the basic example on how to use this effectively.
Works with Routers
var express = ;var router = express;// with routervar longpoll = router; longpoll; router; moduleexports = router;
Can publish to any endpoint, from anywhere.
server.js - create here
var longpoll = app;longpoll;
route.js - use here
var longpoll = router;// Can publish to any endpointlongpoll;
Using Promises
longpoll ; longpoll ;
Sample clientside code to subscribe to the longpoll
Client using jQuery
var { $; }; ; ;