socket-io-queue is a windowed, acknowledgement based message queue using socket.io as a transport. It uses a configurable buffer and window and supports retransmits, pausing and resuming. The server module runs in Node and the client module can be used either in Node or in a browser (e.g. using Browserify).
The queue is acknlowdgement based in that the server ooly sends a certain number of messages (known as the window
, which is configurable by the user) without receiving an acknlowledgement from the client. When the client sends an acknowledgement, if the server is waiting it will resume sending messages, if the server isn't waiting it updates to internally reset the window. During operation the server buffers messages which are cleared once an appropriate acknlowedgement is recevied. The size of this buffer is configurable by the user and once the buffer fills up, the connection is closed and an error event emitted in the server.
The queue allows for the easy implementation of client/server architecture with intelligent flow control based on the rate at which the client can process messages.
To create a message server:
var Connection = require('./')('server');
var io = require('socket.io')();
io.on('connection', function(socket){
var connection = new Connection(socket, window_size, buffer_size);
});
io.listen(9876);
To create a client:
var Client = require('.')('client');
var client = new Client('ws://localhost:9876', {});
Exposed by require('/.')('client')
.
Client(url, opts)
constructs the client module using parametersurl
defines the URL to connect toopts
(optional) an optional set of parameters which are passed to the socket.io client constructor
retransmit()
requests the server to retransmit all messages since the client last sent an acknowledgementsetWindow(windowSize)
requests the server to change the window size towindowSize
pause()
requests the server to pause the streamresume()
requests the server to resume the streamisConnected()
returns whether the client is currently connectedclose()
closes the clientdebug(debugObj)
sendsdebugObj
to the server using the debug channel
Exposed by require('/.')('server')
.
Connection(socket, windowSize, maxBufferSize)
constructs the client module using parameterssocket
a socket.io server socketwindowSize
the initial acknowledgement window sizemaxBufferSize
the server's buffer size
getConnectionID()
returns a unique internal connection identifiergetWindowSize()
returns the current acknowledgement window sizeisWaiting()
returns whether the server is currently waiting on an acknowledgement before sending more dataisPaused()
returns whether the stream is currently pausedpushData(dataObj)
pushesdataObj
into the server's send buffer
The client and server modules emit events.
connect
on client connectdisconnect
on client disconnectdata
on receipt of a data message, with parameterclient_message
a Client Message object which has methodsgetData
returns the user data sent from the servergetSequence
returns the sequence number of the messageneedsAck
returns whether the server requires an acknlowedgement for this message (note that the user shouldn't care about this, and should just use thedone
method)done
should be called when the client has finished processing the message, internally this will, if required, send an acknowledgement to the server
error
on receipt of an error message, with parametererror_obj
the error object received
debug
on receipt of a debug message, with parameterdebug_obj
the debug object received
control
on receipt of a control response message (i.e. response to a control request), with parametercontrol_response_obj
the control response object received
disconnect
on client disconnectack
on receipt of an acknowledgment, with parametersequence
the sequence number which the client is acknowledging
control
on receipt of a control messagecontrol_response_obj
the control object received
debug
on receipt of a debug message, with parameterdebug_obj
the debug object received
Plans to add features to support namespaces, elegant multiplexing and a simple 'query' protocol from client to server.