bacon-socket.io

0.1.3 • Public • Published

bacon-socket.io

Wraps socket.io server side socket handling as Bacon EventStreams

Usage

If you have socket.io client code like this:

  var socket = io.connect("http://localhost:3030")
  socket.on("connect", function() {
    var counter = 0;
    setInterval(function() {
      socket.emit("hello", "World! " + ++counter)
    }, 1000)
  })

You can listen to hello events from all sockets like this:

var server = require('socket.io')()
var sockets = require('bacon-socket.io')(server)

sockets.listenToAllSocketsOfType('hello').onValue(function(socketEvent) {
  console.log(socketEvent.socket.id + ' ' + socketEvent.socketData)
})

This will print out:

% node index.js
// user connects...
2Oh53qybapeIRp27AAAF World! 1
2Oh53qybapeIRp27AAAF World! 2
2Oh53qybapeIRp27AAAF World! 3
1IfF0ZFJOjTls5IvAAAG World! 1
1IfF0ZFJOjTls5IvAAAG World! 2
1IfF0ZFJOjTls5IvAAAG World! 3
1IfF0ZFJOjTls5IvAAAG World! 4
1IfF0ZFJOjTls5IvAAAG World! 5
1IfF0ZFJOjTls5IvAAAG World! 6
1IfF0ZFJOjTls5IvAAAG World! 7
1IfF0ZFJOjTls5IvAAAG World! 8
1IfF0ZFJOjTls5IvAAAG World! 9
1IfF0ZFJOjTls5IvAAAG World! 10

If you want to send messages to the client, use the socket provided by the stream, like this:

sockets.listenToAllSocketsOfType('hello').onValue(function(socketEvent) {
  socketEvent.socket.emit('hello', 'whaddup!')
})

Which you can then again listen to with the socket.io client in the browser:

socket.on("hello", function(x) {
  console.log('got a response: ' + x) // got a response: whaddup!
})

Install

Install from npm:

npm install bacon-socket.io

License

Licensed under the MIT License.

Readme

Keywords

none

Package Sidebar

Install

npm i bacon-socket.io

Weekly Downloads

3

Version

0.1.3

License

MIT

Last publish

Collaborators

  • antris