MessageCenter is a transfer layer agnostic RPC protocol base on json. It's a more human friendly protocol than machine friendly one.
Feature:
- transparent javascript type serialize (Buffer,Date)
- support reliable RPC call and not reliable event dispatch.
- support nodejs streaming with easy to use API.
To use this protocol the underlying transfer layer should meet these requirements.
- Can send block of data.
- Each block of data should either be completely recieved without any mistake or completely droped.
Each block of data is a JSON with some special field, I will describe it latter and let us see some example of using node-message-center with ws - a great websocket implementation in nodejs.
Install and test
# install npm install message-center# test # you will need mocha to run the test # you can get mocha with (sudo) npm install -g mocha cd node_modules/message-centernpm test
Example
WebSocket = ;WebSocketServer = WebSocketServer;MessageCenter = ; mcClientSide = ;mcServerSide = ; server = hostname:"localhost"port:12345server connection = "ws://localhost:12345/";connectionconnection mcClientSidemcServerSide
MessageCenter has a default timeout of 60 * 1000 ms, if you don`t like it you can set it manually.
var invoke = mcClientSideinvoke;//or set for all invokesmcClientSidetimeout = 3000;//every request has a 3000 timeout.
MessageCenter also support event dispatching without promising.
mcServerSide//Note here we have a "event/" prefix so we don't always conflict with local events by accidents.mcClientSide
Streaming
mcServerSidemcClientSide
General error handle designs:
- Any error caused by underlying connection will just fail silently. (say throw error in send)
- Any error caused by broken data will emit an error event. (likely broken connection or some violation, you'd better unsetConnection immediately)
- Other exception like broken paramaters will just throw. (likely due to unmatched MessageCenter version)