snapper-consumer
snapper consumer client for node.js and browser.
Snapper-consumer
is tested in snapper-core
Snapper-core Teambition push messaging service
Demo
# module: socket.coffee define Backbone = require'backbone' Consumer = require'consumer' http = require'lib/http-thunks' : if eventtype isnt 'request' return @onerror'It is not a request: ' + JSONstringifyeventdata # parse multi sub-messages while eventdataparamslength try data = JSONparseeventdataparamsshift if data and typeof datae is 'string' @triggerdataedatad else @onerror'Unprocessable entity: ' + JSONstringifydata catch err @onerrorerr : # join a room by API server httppost"/subscribe"consumerId: consumerId _extendSocketClientprototypeBackboneEvents return # --------------------------- # connect to server socketClientconnectteambitionpushHost path: '/websocket' token: teambitionuserget'snapperToken' # listen for messages socketClienton'xxx'
API
var Consumer =
new Consumer([url][, options])
var consumer =
url
:String
, Snapper server host.options
:Object
,engine.io-client
options, but addedoptions.token
.
consumer.prototype.onopen()
consumer.prototype.onclose()
consumer.prototype.onerror(error)
error
:Error Object
.
Default error listener, overwrite it in production.
consumer.prototype.onmessage(event)
event.id
:String|Number|null|undefined
, JSON-RPC idevent.data
:Object
, JSON-RPC object.event.type
:String
, JSON-RPC type,'invalid'
,'notification'
,'success'
,'error'
,'request'
.
Default messages listener, overwrite it in production.
consumer.prototype.getToken()
Default getToken
, overwrite it in production.
Consumerprototype { return thisoptionstoken}
consumer.prototype.request(method[, params][, callback])
method
:String
, JSON-RPC request method.params
:Mixed
, JSON-RPC request params.callback
:function
, response callback.
Send a JSON-RPC request to server.
consumer.prototype.join(room), consumer.prototype._join(room, consumerId)
room
:String
, room name.
_join
method should be implemented for this method.
consumer.prototype._respond(event)
It is used to respond server's request.
consumer.prototype.connect([url][, options])
url
:String
, Snapper server host.options
:Object
,engine.io-client
options, but addedoptions.token
.
Connect to server. The arguments is the same as constructor, should be provided in constructor or here.
consumer.prototype.close()
Close the client.