eims-hub v0.4.5
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 assistin 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
optsObjectopts.urlServer endpoint hosting websocket
opts.subscribePathstring Server side message endpoint that services channel subscriptions. (optional, default'/api/join')opts.factornumber Backoff step factor ("to be increased by") (optional, default200)opts.maxnumber Ceiling for backoff wait time (optional, default2000)
write
Serialize and write data to WebSocket. Given to the WebSocket instance but bound to the Hub instance.
Parameters
dataany 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
reqobject Request object to be written to WebSocket. Will be deocorated withuuid.v1andpending = 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
channelstring Name of channel, to be send onbody.channelproperty of outgoing message.topicstring Optional topic in channel, could be used as a sort of subcategory of interest.cbsubscribeCallback 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
channelstring Name of channel to unsub from.cbfunction 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
msgDataobject message coming from server