1.0.0 • Published 3 years ago

@dsps/client v1.0.0

Weekly downloads
37
License
MIT
Repository
github
Last release
3 years ago

DSPS (Durable & Simple PubSub) JavaScript client

DSPS client library for JavaScript environments such as web browser or Node.js.

3 minutes to start

  1. npm add @dsps/client or yarn add @dsps/client
  2. Copy & edit sample code below:
import { Dsps } from "dsps";

// Create DSPS client
const dsps = new Dsps({
  http: {
    // You can use absolute URL (e.g. "https://dsps.examplem.com/path-prefix") or relative URL
    baseURL: `url-to-dsps`,
  },
});

// Send message
dsps.channel("my-channel").publish(
  null, // null means automatically generate messageID
  { hi: "Hello!" } // Content of the message, OK to pass any JSON
);

// Subscribe messages
const subscription = dsps.channel("my-channel").subscribe({
  callback: async (messages) => {
    console.log("I got messages", messages); // Do anything you want
  },
  abnormalEndCallback: (e) => {
    // Please handle this error (e.g. navigate to login screen with error message)
    console.error(`Subscription abnormally ended due to ${e.code} (;﹏;)`, e);
  },
});

// (Optional) If you lose interest in new messages, call close() method to stop subscription
subscription.end();

Error handling

To handle errors, you can register listeners to the dsps client:

const dsps = new Dsps({
  /* ... */
});

// Listen API call failures:
dsps.addEventListener("apiFailed", (e) => {
  // Do anything you want...
});

// Listen errors thrown by your subscription callbacks:
dsps.addEventListener("subscriptionCallbackError", (info) => {
  // Do anything you want...
});

To check full list of listeners, see type definition of the client interface.

FAQ

Where is @types package for TypeScript?

Package includes .d.ts type definition files. No need to install type package additionally.

What configuration/customization supported?

Check type definition of the config object for the full list of configuration properties.

1.0.0

3 years ago

1.0.0-alpha.0

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.0-alpha.1

3 years ago