How to use RxJs with Socket.IO on event

后端 未结 6 905
温柔的废话
温柔的废话 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:43

    ES6 one liner that i use, using ES7 bind syntax:
    (read $ as stream)

    import { Observable } from 'rxjs'
    
    // create socket
    
    const message$ = Observable.create($ => socket.on('message', ::$.next))
    // translates to: Observable.create($ => socket.on('message', $.next.bind(this)))
    
    // filter example
    const subscription = message$
      .filter(message => message.text !== 'spam')
      //or .filter(({ text }) => text !== 'spam')
      .subscribe(::console.log)
    

提交回复
热议问题