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

@awly/express-longpoll

v1.0.0

Published

Easy long polling for express.

Downloads

7

Readme

Build Status npm version

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 = require('express');
var app = express();
var longpoll = require("express-longpoll")(app)
// You can also enable debug flag for debug messages
var longpollWithDebug = require("express-longpoll")(app, { DEBUG: true });

Quick-start code

Server - server.js

var express = require('express');
var app = express();
var longpoll = require("express-longpoll")(app);

// Creates app.get("/poll") for the long poll
longpoll.create("/poll");

app.listen(8080, function() {
    console.log("Listening on port 8080");
});

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 /poll
longpoll.publish("/poll", data);

// Publish every 5 seconds
setInterval(function () { 
    longpoll.publish("/poll", data);
}, 5000);

Client - index.js (with jQuery)

var poll = function () {
    $.ajax({
       url: "localhost:8080/poll",
       success: function(data){
           console.log(data); // { text: "Some data" } -> will be printed in your browser console every 5 seconds
           poll();
       },
       error: function() {
           poll();
       },
       timeout: 30000 // 30 seconds
    });
};

// Make sure to call it once first,
poll();

###longpoll.create(url, [options])
Sets up an express endpoint using the URL provided.

var longpoll = require("express-longpoll")(app);

longpoll.create("/poll");
longpoll.create("/poll2", { maxListeners: 100 }); // set max listeners

###longpoll.create(url, middleware, [options])
Set up an express endpoint using the URL provided, and use middleware.

var longpoll = require("express-longpoll")(app);

longpoll.create("/poll", function (req,res,next) {
    // do something
    next();
});

###longpoll.publish(url, data)
Publishes data to all listeners on the url provided.

var express = require('express');
var app = express();
var longpoll = require("express-longpoll")(app);

longpoll.create("/poll");

longpoll.publish("/poll", {});
longpoll.publish("/poll", { hello: "Hello World!" });
longpoll.publish("/poll", jsonData);

###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 = require('express');
var router = express.Router();
// with router
var longpoll = require("express-longpoll")(router);

longpoll.create("/routerpoll");

router.get("/", (req, res) => {
    longpoll.publish("/routerpoll", {
        text: "Some data"
    });
    res.send("Sent data!");
});

module.exports = router;

Can publish to any endpoint, from anywhere.

server.js - create here

var longpoll = require("express-longpoll")(app);
longpoll.create("/poll");

route.js - use here

var longpoll = require("express-longpoll")(router);
// Can publish to any endpoint
longpoll.publish("/poll");

Using Promises

longpoll.create("/poll")
  .then(() => {
    console.log("Created /poll");
  })
  .catch((err) => {
    console.log("Something went wrong!", err);
  });
  
longpoll.publish("/poll", data)
  .then(() => {
    console.log("Published to /poll:", data);
  })
  .catch((err) => {
    console.log("Something went wrong!", err);
  });

Sample clientside code to subscribe to the longpoll

###Client using jQuery

var subscribe = function(url, cb) {
        $.ajax({
            method: 'GET',
            url: url,
            success: function(data) {
                cb(data);
            },
            complete: function() {
                setTimeout(function() {
                    subscribe(url, cb);
                }, 1000);
            },
            timeout: 30000
        });
    };
    
subscribe("/poll", function (data) {
  console.log("Data:", data);
});

subscribe("/poll2", function (data) {
  console.log("Data:", data);
});