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

websocket-response-tester

v2.0.1

Published

l

Downloads

13

Readme

Introduction

Effectively test asynchronous message events from websocket connections. Only tested with socket.io.

The difficulty involved with testing websocket message events arise in particular during indirect trigger events. This occurs when a message is sent to the socket not as a result of a write from the socket itself. Examples of use cases are:

  • Socket A writes to its counterparty socket, which then receives the message and sends out messages to sockets B, C and D. In this case, we must test that sockets B, C and D have received the message.
  • An HTTP call from a secondary source - maybe an admin user wishing to trigger an event - results in several sockets receiving messages. Again, we have to test that these sockets have received the correct messages.

The ideal scenario in the above is

  1. The sockets have listeners attached
  2. An event is triggered that sends messages to the above sockets
  3. Somehow catch the messages and only proceed if all messages are received.

One of the ways to deal with this flow is by the use of setTimeout in Node.js to check periodically for any changes in non-local state variables to which any messages from the sockets are diverted. The downsides of this approach are

  • The need for waiting time while polls take time to happen.
  • The potential for pollution of higher scope environments.
  • The need for custom code to hook up messages to the respective clients at the start, then remove them after the third-party trigger has done its work.

A more elegant way to approach the problem is via the use of delayed promises, which this module uses.

Installation

npm install websocket-response-tester --save-dev

Simple Usage

var WRT = require('websocket-response-tester')

var socket = require('socket.io-client')('http://localhost:3000')

WRT()
.registerSockets({
	mySocket: socket
})
.addEventWaiter('mySocket', 'message')
.queueFunction(() => {
	// do something that should trigger messages being received by the socket

})
.then((responses) => {
	console.log(responses) 
})

Then is only called after the socket has received the stated event. If the socket fails to receive a message, then is never called, and the code just stalls and waits indefinitely. This is not an issue for mocha test cases, which have a timeout of 2000ms.

// Responses are of the form
{
	socketName1: {
		eventTitle1: [data]
	},
	socketName2: {
		eventTitle2: [data, data],
		eventTitle3: [data]
	}
}

Advanced Usage

  • Each socket can have multiple event waiters attached.
  • Event waiters can be stacked, i.e. one socket can wait for multiple events of the same kind. Then only fires after all of the attached events are returned. In the above example, socket1 and socket2 will wait for 2 'message' events each, while socket3 will wait for 1 'message' event and 1 'news' event.
var WRT = require('websocket-response-tester')

var socket1 = require('socket.io-client')('http://localhost:3000')
var socket2 = require('socket.io-client')('http://localhost:3000')
var socket3 = require('socket.io-client')('http://localhost:3000')

WRT()
.registerSockets({
	name1: socket1,
	name2: socket2,
	name3: socket3
})
.addEventWaiter(['name1', 'name2', 'name3'], 'message')
.addEventWaiter(['name1', 'name2'], 'message')
.addEventWaiter('name3', 'news')
.queueFunction(() => {
	// do something that should trigger messages being received by the socket

})
.then((responses) => {
	console.log(responses) 
})

Testing

Fork and download this repository.

cd websocket-response-tester
npm test

This spins up a node.js server that

  • Starts up a server with some server side socket.io code
  • Starts up 3 client sockets that connect to the server
  • Requires this module
  • Uses this module to send messages to the server and test the messages sent back to the client side sockets

Contributions

To-do:

  • timer option that throws an error, so that the code doesn't stall and wait indefinitely.

Contributions are welcome. For questions, please contact me at [email protected]