Coder Social home page Coder Social logo

bacon-socket.io's Introduction

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.

bacon-socket.io's People

Contributors

laurilehmijoki avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.