项目作者: theY4Kman

项目描述 :
Consume channels-api websocket services
高级语言: JavaScript
项目地址: git://github.com/theY4Kman/channels-api-client.git
创建时间: 2017-03-16T20:01:18Z
项目社区:https://github.com/theY4Kman/channels-api-client

开源协议:

下载


channels-api-client

Build Status npm version

This package aims to provide a simple, reliable, and generic interface to consume channels-api powered WebSocket APIs.

Features

  • Promises encapsulating the request/response cycle
  • Subscribe to updates with a callback
  • Automatically reconnect when connection is broken (with backoff — thanks to reconnecting-websocket)
  • Automatically restart subscriptions on reconnection
  • Requests are queued until a connection is made (no need to wait for connection before sending requests)

Install

  1. npm install --save @usslc/channels-api

Usage

  1. const channelsApi = require('channels-api');
  2. const client = channelsApi.connect('wss://example.com');
  3. client.create('people', {name: 'Alex'}).then(person => {
  4. console.info('Created:', person);
  5. });
  6. client.retrieve('people', 4).then(person => {
  7. console.info('Retrieved person 4:', person);
  8. });
  9. client.update('people', 4, {name: 'Johannes'}).then(person => {
  10. console.info('Changed name of person 4. Properties after change:', person);
  11. });
  12. client.delete('people', 4).then(() => {
  13. console.info('Deleted person 4. No one liked them, anyway :)');
  14. });
  15. // Subscribe to updates to any person
  16. const subscription = client.subscribe('people', 'update', person => {
  17. console.info('A person was updated:', person);
  18. });
  19. // Stop listening for updates
  20. subscription.cancel();
  21. // Subscribe to updates to person 1
  22. const personalSubscription = client.subscribe('people', 'update', 1, person => {
  23. console.info('Person 1 was updated:', person);
  24. });
  25. // Stop listening
  26. personalSubscription.cancel();
  27. // Make a generic request to a multiplexer stream
  28. client.request('mystream', {key: 'value'}).then(response => {
  29. console.info('Got mystream response, yo:', response);
  30. });

Configuration

The client can be customized by passing an object as the second argument to connect() or createClient(). The available options are described below.

  1. const client = channelsApi.connect('wss://example.com', {
  2. preprocessPayload: (stream, payload, requestId) => {
  3. // Modify payload any way you see fit, before it's sent over the wire
  4. // For instance, add a custom authentication token:
  5. payload.token = '123';
  6. // Be sure not to return anything if you modify payload
  7. // Or, you can overwrite the payload by returning a new object:
  8. return {'this': 'is my new payload'};
  9. },
  10. preprocessMessage: (message) => {
  11. // The "message" is the final value which will be serialized and sent over the wire.
  12. // It includes the stream and the payload.
  13. // Modify the message any way you see fit, before its sent over the wire.
  14. message.token = 'abc';
  15. // Don't return anything if you modify message
  16. // Or, you can overwrite the the message by returning a new object:
  17. return {stream: 'creek', payload: 'craycrayload'};
  18. },
  19. // Options to be passed to ReconnectingWebsocket
  20. // See https://github.com/pladaria/reconnecting-websocket#configure for more info
  21. websocket: {
  22. constructor: isGlobalWebSocket() ? WebSocket : null,
  23. maxReconnectionDelay: 10000,
  24. minReconnectionDelay: 1500,
  25. reconnectionDelayGrowFactor: 1.3,
  26. connectionTimeout: 4000,
  27. maxRetries: Infinity,
  28. debug: false,
  29. }
  30. });