0.4.5 • Published 7 years ago

eims-hub v0.4.5

Weekly downloads
1
License
MIT
Repository
-
Last release
7 years ago

Hub

hoob

Here's a simple client side websocket utililty tool. Create a websocket, audo de/serialize messages across the sockect, auto reconnect with stepped backoff, manage pub/sub style subscribtions to channels on a corresponding server.

Uses eims-rest-contract to standardize read/write message payloads and assist in some bookeeping involing message ids, timestamps, and the like. lies

Tests

Gargabe. Tried using Karma to test in like, every friggin browser man it's awesome and it kinda worked. Worth circling back on.

Might work up a suite and mock the WebSocket, but that may be more beneficial as practice as anything.

API

Hub

hub constructor, sets up config for backoff, WebSocket creation, internal message buffer, does some light sanity checking.

Parameters

  • opts Object
    • opts.url string Server endpoint hosting websocket
    • opts.subscribePath string Server side message endpoint that services channel subscriptions. (optional, default '/api/join')
    • opts.factor number Backoff step factor ("to be increased by") (optional, default 200)
    • opts.max number Ceiling for backoff wait time (optional, default 2000)

write

Serialize and write data to WebSocket. Given to the WebSocket instance but bound to the Hub instance.

Parameters

  • data any serializeable object to be sent across socket

request

Write a message to the server expecting a response to come back keyed with the same requestId as aformentioned write.

Parameters

  • req object Request object to be written to WebSocket. Will be deocorated with uuid.v1 and pending = true.

Returns Promise Rejects if error property present on payload from server, resolves otherwise.

subscribe

Initiate write to WebSocket to communicate interest in subscribing to messages across a given channel. Pass subscribePath as option to constructor to configure "endpoint" used to initiate subscription on server.

Parameters

  • channel string Name of channel, to be send on body.channel property of outgoing message.
  • topic string Optional topic in channel, could be used as a sort of subcategory of interest.
  • cb subscribeCallback Callback to be invoked on each message written to channel by server.

unsubscribe

Send message to server indicating desire to unsubscribe from given channel.

Parameters

  • channel string Name of channel to unsub from.
  • cb function Reference to the callback originally passed on subscription creation.

prototype

Create a new websocket and hold the ref to it internally. Also decorates the new WebSocket with a write method to serialized outgoing messages.

subscribeCallback

Type: Function

Parameters

  • msgData object message coming from server
0.4.5

7 years ago

0.4.4

7 years ago

0.4.3

7 years ago

0.4.2

7 years ago

0.4.1

7 years ago

0.4.0

7 years ago

0.1.0

7 years ago