Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use RxJs with Socket.IO on event

Tags:

I want to use RxJS inside of my socket.on('sense',function(data){});. I am stuck and confused with very few documentation available and my lack of understanding RxJS. Here is my problem.

I have a distSensor.js that has a function pingEnd()

function pingEnd(x){ socket.emit("sense", dist); //pingEnd is fired when an Interrupt is generated. } 

Inside my App.js I have

io.on('connection', function (socket) {     socket.on('sense', function (data) {         //console.log('sense from App4 was called ' + data);     }); }); 

The sense function gets lots of sensor data which I want to filter using RxJS and I don't know what should I do next to use RxJs here. Any pointers to right docs or sample would help.

like image 759
Mitul Avatar asked Apr 23 '15 04:04

Mitul


People also ask

How do I broadcast an event in socket IO?

To broadcast an event to all the clients, we can use the io. sockets. emit method. Note − This will emit the event to ALL the connected clients (event the socket that might have fired this event).

How do I broadcast a message to a room is socket IO?

referer; // link of the page, where user connected to socket // connecting to room socket. join(room_name, function(){ //trying to send messages to the current room io.to(room_name, function () { socket. on('chat message', function (msg) { io. emit('chat message', msg); }); }); }); });

What is the difference between WebSocket and socket IO?

Key Differences between WebSocket and socket.ioIt provides the Connection over TCP, while Socket.io is a library to abstract the WebSocket connections. WebSocket doesn't have fallback options, while Socket.io supports fallback. WebSocket is the technology, while Socket.io is a library for WebSockets.


1 Answers

You can use Rx.Observable.fromEvent (https://github.com/Reactive-Extensions/RxJS/blob/master/doc/api/core/operators/fromevent.md).

Here's how I did a similar thing using Bacon.js, which has a very similar API: https://github.com/raimohanska/bacon-minsk-2015/blob/gh-pages/server.js#L13

So in Bacon.js it would go like

io.on('connection', function(socket){   Bacon.fromEvent(socket, "sense")     .filter(function(data) { return true })     .forEach(function(data) { dealWith(data) }) }) 

And in RxJs you'd replace Bacon.fromEvent with Rx.Observable.fromEvent.

like image 86
raimohanska Avatar answered Sep 24 '22 05:09

raimohanska