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

rx-socket-subject

v0.7.0

Published

A more full-featured wrapper for web sockets using RxJS and RxJS-DOM

Downloads

9

Readme

RxSocketSubject

Build Status

A more advanced web socket wrapper for RxJS

Install

With bower:

bower install -S rx-socket-subject

With npm:

npm install -S rx-socket-subject

Goals

The goals of this project is to produce an observable WebSocket implementation that meets a set of common needs for projects I'm currently working on. RxJS-DOM has a fine WebSocket implementation, which I modelled the initial work on this implementation off of. However, I need something that also does the following:

  • Socket that will automatically reconnect itself.
  • Socket that will automatically try additional endpoints.
  • Has seperate hooks for open, close, and error.
  • Will send proper close messages with WebSocket close codes to the server when onError is called.
  • Will send a close command to the server when onCompleted is called.
  • Will always force a single instance of a socket regardless of the number of subscriptions.
  • Will buffer messages fed to it via onNext if the underlying socket isn't open, send them all when it does open.

Basic Usage

// create a socket subject
// at this point an underlying WebSocket has not yet been created
var socket = RxSocketSubject.create('ws://echo.websocket.org');

// use the socket subject as an Observer and send
// messages with onNext()
// even though the underlying socket still hasn't been created,
// the messages will be buffered until a socket is made and connected
socket.onNext('one');
socket.onNext('two');

// subscribing to the socket subject as an Observable
// will start the socket and connect.
socket.forEach(function(e) {
	console.log(e); // raw message events are emitted
});

setTimeout(function(){
  // while the socket is connected, onNext will send the message
  // immediately through it.
	socket.onNext('three');
}, 1000);

Closing the socket

var socket = RxSocketSubject.create('ws://echo.websocket.org');

var disposable = socket.forEach(function(e) {
  console.log(e.data);

	if(e.data === 'two') {
		// use onCompleted to gracefully close the socket
		socket.onCompleted();
	}
});

socket.onNext('one');
socket.onNext('two');

Forcing an error close on the socket

There are some cases where you'll want to acknowledge what you've received from the server was an error. With a normal web socket, this is generally done with socket.close(code, reason). RxSocketSubject allows for this by leveraging onError when the subject is used as an Observable.

More information about WebSocket status codes can be found on MDN.

var socket = RxSocketSubject.create('ws://echo.websocket.org');

var disposable = socket.forEach(function(e) {
  console.log(e.data);

	if(e.data === 'bad data') {
		// use onError to emit an error and send a close
		// command to the server with a status code and reason:
		// The following will send a status code of 1008 (a generic error)
		// as well as a reason "bad data received"
		socket.onError('bad data received');
	}

	if(e.data === 'unparsable data') {
		// you can also specify a custom error code with the 
		// ClientInitiatedError class
		socket.onError(new RxSocketSubject.ClientInitiatedError('unable to parse data', 4001));
	}

	if(e.data === 'wat') {
		// or you can just onError
		socket.onError();
	}
});

Using fallback endpoints

// it's as simple as providing an array of endpoints to the `create()` method
// in order to have the socket fallback to another endpoint if connection fails
// or if an error closes the connection. The Subject will keep retrying each
// endpoint until one connects successfully

var socket = RxSocketSubject.create(['ws://benlesh.com/notreal', 'ws://benlesh.com/totallyfake', 'ws://echo.websocket.org']);