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.
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).
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); }); }); }); });
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With