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

socket-requests

v0.1.2

Published

Multi-response requests using socket.io

Downloads

10

Readme

socket-requests

A simple wrapper around socket.io for sending requests that generate multiple responses.

Installation

npm install socket-requests

Usage

Set up your socket.io like normal. Then do this:

// client
var request = require('socket-requests').request;
var socket = io('http://localhost:5000/'); // as usual

request(socket, 'ping-pong', { query: "Ping" }, function (response) {
	console.log(response);
}).then(function (responses) {
	console.log(responses);
});
// server
var listen = require('socket-requests').listen;

io.on('connection', function (socket) {
	listen(socket, 'ping-pong', (request, send) => {
		send(request.query + ' pong.', true);

		setTimeout(function () {
			send('Puff!');
		}, 1000);
	});
});

The above example will print the following on the client:

Ping pong.
Puff!
[ "Ping pong." , "Puff!" ]

API

function listen(socket, name, handler)

This is the server-side part. Listen on the socket for events of type name, sending their data to the handler.

Multiple responses via send callback

The handler receives a callback for replying to the request as the second parameter. The callback is of signature

function send (result, keepalive = false) { /* ... */ }

If keepalive is true, there are more responses to come, otherwise the client is notified about the end of the response stream.

Single reponse via return value or promise

If the handler returns a value, this value is sent to the client. The handler may also return a promise, whose return value is sent to the client upon resolution. Both cases terminate the response stream, so return undefined in your handler if you use the send callback asynchronously and cannot make sure to resolve your handler Promise only after you have sent your last response.

function request(socket, name, request [, partialResultHandler] )

This function enriches the passed request data with a timestamp and sends it through the socket. Every subsequent response on that socket matching event that contains the original request's timestamp is considered a reply to the request. All responses are collected in an array. The function returns a promise that is resolved with the array of responses once the server sends no keepalive, hence signals the response stream to terminate.

If a partialResultHandler is supplied, it is called once for every reponse to the request.

Usage without Socket.IO

Since this is a simple wrapper using only .on, .off and .emit functions, you can simply use this library with any node standard EventEmitter, as seen in the examples.