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

rockets-client

v1.0.0

Published

> A small client for [Rockets](../README.md) using [JSON RPC](https://www.jsonrpc.org) as communication contract over a [WebSocket](https://developer.mozilla.org/en-US/docs/Web/API/WebSocket).

Downloads

62

Readme

Rockets JS Client

A small client for Rockets using JSON RPC as communication contract over a WebSocket.

Travis CI Language grade: JavaScript

Table of Contents

Installation


You can install this package from NPM:

npm add rxjs rockets-client

Or with Yarn:

yarn add rxjs rockets-client

CDN

For CDN, you can use unpkg:

https://unpkg.com/rockets-client/dist/bundles/rockets-client.umd.min.js

The global namespace for rockets is rocketsClient:

const {Client} = rocketsClient;

const rockets = new Client({
    url: 'myhost'
});

Usage


Connection

Create a client and connect:

import {Client} from 'rockets-client';

const rockets = new Client({
    url: 'myhost',
    onConnected() {
        console.info('I just connected');
    },
    onClosed(evt) {
        console.log(`Socket connection closed with code ${evt.code}`);
    }
});

Close the connection with the socket cleanly:

import {Client} from 'rockets-client';

const rockets = new Client({url: 'myhost'});

rockets.subscribe({
    next(notification) {
        console.log(notification);
    },
    complete() {
        console.log('Socket connection closed');
    }
});

rockets.disconnect();

Notifications

Listen to server notifications:

import {Client} from 'rockets-client';

const rockets = new Client({url: 'myhost'});

rockets.subscribe(notification => {
    console.log(notification);
});

Send notifications:

import {Client} from 'rockets-client';

const rockets = new Client({url: 'myhost'});

rockets.notify('mymethod', {
    ping: true
});

Send a notification using the Notification object:

import {Client, Notification} from 'rockets-client';

const rockets = new Client({url: 'myhost'});

const notification = new Notification('mymethod', {
    ping: true
});
rockets.notify(notification);

Requests

Make a request:

import {Client} from 'rockets-client';

const rockets = new Client({url: 'myhost'});

const response = await rockets.request('mymethod', {
    ping: true
});
console.log(response);

NOTE: There is no need to wait for a connection to be established with the socket as requests will be buffered and sent once the connection is alive. In case of a socket error, the request promise will reject. The same is true for batch requests and notifications.

Or make a request using the Request object:

import {Client, Request} from 'rockets-client';

const rockets = new Client({url: 'myhost'});

const request = new Request('mymethod', {
    ping: true
});
const response = await rockets.request(request);
console.log(response);

Handle a request error:

import {Client} from 'rockets-client';

const rockets = new Client({url: 'myhost'});

try {
    await rockets.request('mymethod');
} catch (err) {
    console.log(err.code);
    console.log(err.message);
    console.log(err.data);
}

NOTE: Any error that may occur will be a JsonRpcError.

Cancel a request:

import {Client} from 'rockets-client';

const rockets = new Client({url: 'myhost'});

const task = rockets.request('mymethod');
task.cancel();

Get progress updates for a request:

import {Client} from 'rockets-client';

const rockets = new Client({url: 'myhost'});

const task = rockets.request('mymethod');
task.on('progress')
    .subscribe(progress => {
        console.log(progress);
    });

Batching

Make a batch request:

import {Client, Notification, Request} from 'rockets-client';

const rockets = new Client({url: 'myhost'});

const request = new Request('mymethod');
const notification = new Notification('mymethod');
const [response] = await rockets.batch(...[
    request,
    notification
]);

const result = await response.json();
console.log(result);

NOTE: Notifications will not return any responses and if no requests are sent, the batch request will resolve without any arguments.

Handle a batch request error:

import {Client} from 'rockets-client';

const rockets = new Client({url: 'myhost'});

try {
    const request = new Request('mymethod');
    await rockets.batch(request);
} catch (err) {
    console.log(err.code);
    console.log(err.message);
    console.log(err.data);
}

NOTE: The batch promise will not reject for response errors, but you can catch the response error when using the response .json() method:

import {Client} from 'rockets-client';

const rockets = new Client({url: 'myhost'});

try {
    const request = new Request('mymethod');
    const [response] = await rockets.batch(request);
    await response.json();
} catch (err) {
    console.log(err.code);
    console.log(err.message);
    console.log(err.data);
}

Cancel a batch request:

import {Client} from 'rockets-client';

const rockets = new Client({url: 'myhost'});

const request = new Request('mymethod');
const task = rockets.batch(request);

task.cancel();

NOTE: Notifications cannot be canceled.

Get progress updates for a batch request:

import {Client} from 'rockets-client';

const rockets = new Client({url: 'myhost'});

const request = new Request('mymethod');
const task = rockets.batch(request);

task.on('progress')
    .subscribe(progress => {
        console.log(progress);
    });

Release


If you're a contributor and wish to make a release of this package use:

# Cut a minor release
# A release can be: patch, minor, major;
yarn release --release-as minor

See release as a target type imperatively like npm-version for more release options.

After the changes land in master, the new version will be automatically published by the CI.

IMPORTANT: Follow the semver for versioning.

Learning Material