streamitter v0.0.1
Streamitter
Streamitter is a server-to-server event emitter wire protocol. It provides a shared event emitter between a server and client, with basic reconnection and exponential backoff logic.
Install
npm install streamitter
Usage
var server = Streamitter.createServer(function (client) {
client.on('ping', function () {
client.emit('pong')
})
})
server.listen(5555)
var client = Streamitter.createClient(5555)
client.on('pong', function () {
console.log('pong received')
})
client.emit('ping')
Warning
This is a shared emitter between client and server. Accidental infinite loops are possible.
var server = Streamitter.createServer(function (client) {
// All requests initiate a response
client.on('request', function () {
client.emit('response')
})
})
server.listen(5555)
var client = Streamitter.createClient(5555)
// All responses initiate a new request
client.on('respond', function () {
client.emit('request')
})
// Infinite loop and a whole lot of network traffic...
client.emit('request')
While care has been taken to prevent events received from the remote connection getting sent back to the remote connection, I can't save you from your own stupidity when you manually emit an event back. To ensure an event does not get sent to the server, whether it came from the remote connection or not, you can use localEmit
rather than emit
.
API
Streamitter.createServer(port, cb)
Creates a net server that uses an event emitter to communicate. Callback receives a unique emitter for the net socket.
Streamitter.createClient(port , host) returns client emitter
Create a client emitter. The port argument may be a number, a string, or an existing net.connect() instance. It also attaches a few extra methods, as seen below.
client.reconnect()
Forcefully destroys the socket and reconnects.
client.autoReconnect()
Flag the client to automatically reconnect, which an exponential backoff mechanism.
client.disconnect()
Disable auto-reconnection, if enabled, and closes the socket.
Copyright (c) 2013 Stephen Belanger
Licensed under MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 years ago