How to use RxJs with Socket.IO on event

后端 未结 6 907
温柔的废话
温柔的废话 2021-02-02 12:56

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 R

6条回答
  •  误落风尘
    2021-02-02 13:24

    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.

提交回复
热议问题