0.1.3 • Published 8 years ago

bacon-socket.io v0.1.3

Weekly downloads
3
License
MIT
Repository
github
Last release
8 years ago

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.

0.1.3

8 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago

0.0.5

9 years ago

0.0.4

9 years ago

0.0.3

9 years ago

0.0.2

9 years ago

0.0.1

9 years ago