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

rxws

v5.4.2

Published

A RESTful reactive JavaScript implmentation on top of web sockets

Downloads

70

Readme

RxWS

npm version Build Status Code Coverage

RxWS is a RESTful reactive JavaScript implementation on top of web sockets. This includes, GET, POST, PUT, REMOVE (DELETE), PATCH, and HEAD. RxWS guarantees message delivery by generating a correlation id for each message (to and from the server). Both the server and client automatically send an acknowledgement response for each request. If there is no acknowledgement after a timeout, an error is thrown.

RxWS implements a RESTful protocol. You can use any websocket server as long as it implements the same protocol. By default RxWS supports SocketIO with rxws-socketio

Setup

RxWS requires a websocket abstraction layer. By default it supports both SockJS and SocketIO.

import rxws from 'rxws';
import SocketIOBackend from 'rxws-socketio/SocketIOBackend';

rxws.setBackend({
	backend: SocketIOBackend,
	url: 'ws://someurl'
});

rxws.get('users')
  .subscribe(data => console.log)
  .catch(error => console.error)

Example

Performing a GET request:

// Request all users
rxws.get('users')
  .subscribe(data => console.log, error => console.error)
  
// Request a specific user
rxws.get('users', {
  parameters: { 'users': 13 }
})
  .subscribe(data => console.log, error => console.error)
  
// Optionally the request could have been built with
rxws({
  method: 'get',
  resource: 'users',
  parameters: { 'users': 13 }
})
  .subscribe(data => console.log, error => console.error)

Performing a POST request:

// Create a user
rxws.post('users', {
  firstName: 'Johnny',
  lastName: 'Appleseed'
})
  .subscribe(data => console.log, error => console.error)

// Optionally the request could have been built with
rxws({
  method: 'post',
  resource: 'users',
  body: {
    firstName: 'Johnny',
    lastName: 'Appleseed'
  }
})
  .subscribe(data => console.log, error => console.error)

Nested resources:

// Request a comment from a specific post
rxws.get('posts.comments', {
  parameters: { 'posts': 13, 'comments': 15 }
})
  .subscribe(data => console.log, error => console.error)

Custom headers:

// Request all comments from a post
rxws.get('posts.comments', {
  parameters: { 'posts': 13 },
  apiVersion: '1.2.1',
  accessToken: '7fgnasdfvy0afdsjfjdls',
  queryParameters: { include: 'history' }
})  
  .subscribe(data => console.log, error => console.error)

Server Notifications:

// Listen for new posts
rxws.onNotification('newPost')
  .forEach((messageBody) => {
    rxws.get('posts', { parameters: { posts: messageBody.id } })
      .subscribe(data => console.log, error => console.error);
  })

Request Middleware:

// Middleware progress from one another in the order they are defined
rxws.requestUse()
	.subscribe(({req, send, reply, next}) => {
		req.header.resource = 'prefix.' + req.header.resource;
		next();
	}, ({req, err}) => {
		//the error function is currently never called
	});

Response Middleware:

// Middleware progress from one another in the order they are defined
rxws.use()
	.subscribe(({req, res, reply, retry, next}) => {
		res.requestTime = Date.now();
		next();
	});

rxws.use()
	.subscribe(({req, res, reply, retry, next}) => {
		next();
	}, ({req, err}) => {
		// Do something with the error and the request.
	});

rxws.use()
	.subscribe(({req, res, reply, retry, next}) => {
		reply(res);
	});
// Use middleware to retry requests

rxws.use()
	.subscribe(({res, reply, retry, next}) => {
		if (res.header.statusCode === 401) {
			auth.refreshAuthToken()
				.then(() => retry())
		} else {
			reply(res);
		}
	})

Reactive example:

// Try three times to get the data and then return cached data if still fails
var source = rxws.get('url').retry(3).catch(cachedVersion());

var subscription = source.subscribe(
  (data) => {
    // Displays the data from the URL or cached data
    console.log(data);
  }
);

API

rxws.setBackend(options)

rxws.setBackend({
	backend: rxwsBackendImplementation,
	url: string,
	url: (): Observable,
	defaultHeaders?: object,
	requestTransformer?: (request: object, send: Function): null,
	responseTransformer?: (response: object, reply: Function, retry: Function): null,
	timeout?: 10000,
	onConnectionError?: (error: string): null
})

rxws(config): observable

rxws({
  method: 'get',
  resource: 'posts',
  parameters: { 'posts': 13 }
});

rxws.get(resource[, config]): observable

rxws.delete(resource[, config]): observable

rxws.head(resource[, config]): observable

rxws.post(resource[, data[, config]]): observable

rxws.put(resource[, data[, config]]): observable

rxws.patch(resource[, data[, config]]): observable

rxws.onMessage(type: string): observable

config obbject:

{
	resource: string,
	method: string,
	parameters: object,
	data: object,
	extraResources: object,
	queryParameters: object
}

###License ISC License (ISC) Copyright (c) 2016, CanopyTax

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.