New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

rockets-client

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rockets-client

> 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).

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
21
increased by425%
Maintainers
1
Weekly downloads
 
Created
Source

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


Keywords

FAQs

Package last updated on 18 Jan 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc