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

resynkd

v1.0.0

Published

ReSynkd: Observable pattern through WebSockets

Downloads

5

Readme

ReSynkd

build status codacy coverage dependencies npm

Observable pattern through WebSockets.

Motivation

ReSynkd puts together the amazing RxJS library and the WebSocket protocol.

Although RxJS provides the WebSocketSubject class, it seems that anyone can subscribe to but also send through the same webSocketSubject, which I did not like. I wanted a way to put an Observable on the server and subscribe to it from the clients, but also vice versa.

Documentation

The typedoc documentation is available at https://cope.github.io/resynkd/docs/

Demo

See the ./demo/ folder in this repo.

Watch the demo video:

Usage

Keep in mind that the socketsSendMethod is a placeholder for whatever the send method is in the websocket implementation used, and thus this function varies. Most often it is simply socket.send, as is in the demo, but it can also be connection.socket.send or e.target.send, and so on.

Endpoint1 (Observable)

import ReSynkd from "resynkd";
const resynkd = new ReSynkd();
socket.onmessage(message => {
	let consumed = resynkd.message(message, socketsSendMethod.bind(socket));	// IMPORTANT: don't forget to bind!
	if(!consumed) console.log(message); // ...handle non-resynkd messages...
});

const subject = new Subject();
resynkd.addSubject('unique_subject_name', subject);
subject.next('value 1');
subject.next('value 25');
subject.next('value 17');
// etc.

Endpoint2 (Observer)

import ReSynkd from "resynkd";
const resynkd = new ReSynkd();
socket.onmessage(message => {
	let consumed = resynkd.message(message, socketsSendMethod.bind(socket));	// IMPORTANT: don't forget to bind!
	if(!consumed) console.log(message); // ...handle non-resynkd messages...
});

resynkd.subscribe({
	socketId: 'socket-id',					  // must be same value on both socket endpoints
	subjectId: 'unique_subject_name',		  
	send: socketsSendMethod.bind(socket),		// IMPORTANT: don't forget to bind!
	observer: {
		next: (value) => {   // * required!
			// handle value...
		},
		error: (err) => {   // * optional
			// handle error...
		},
		complete: () => {   // * optional
			// handle complete...
		}
   	}
});

The on message processing

The main part of any websocket communication is the on message event. Whether it is socket.on('message', ..., or socket.addEventListener('message', ..., in either case, the handler method receives the websocket message and it must include the resynkd processing in it.

import ReSynkd from "resynkd";
const resynkd = new ReSynkd();
let handler = (message) => {
	let consumed = resynkd.message(message, socketsSendMethod.bind(socket));	// IMPORTANT: don't forget to bind!
	if(!consumed) console.log(message); // ...handle non-resynkd messages...
}

Putting it all together, the most common example looks like below...

Server side (using fastify)
import ReSynkd from "resynkd";
const resynkd = new ReSynkd();
fastify.get('/ws', {websocket: true}, (connection, req) => {
	connection.socket.on('message', message => {
		let consumed = resynkd.message(message, connection.socket.send.bind(connection.socket));   // IMPORTANT: don't forget to bind!
		if(!consumed) console.log(message); // ...handle non-resynkd messages...
	});
});
Client side
const ReSynkd = require('resynkd');
const resynkd = new ReSynkd();

let socket = new WebSocket(YOUR_WS_URI);
socket.onmessage = (e) => {
	let { data: message } = e;
	let consumed = resynkd.message(message, e.target.send.bind(e.target));	 // IMPORTANT: don't forget to bind!
	if(!consumed) console.log(message); // ...handle non-resynkd messages...
};

TODO

See the Kanban board

  • Peer-to-Peer (client-to-client) subscriptions
  • Observables broadcast their subjects- so that observers know what they can subscribe to