javascriptwcfcometsystem.reactivereverse-ajax

Any examples of Reactive Extensions for Javascript + WCF?


I understand the basics behind the Javascript examples, but I'm having difficulty seeing how this will work with WCF. My goal is to enable COMET (HTTP Push) style access to my data, but I'm not sure if Rx is the right technology.

How do I use Javascript Rx Extensions with WCF?


Solution

  • Does WCF support HTTP Push? It's fairly easy to convert an arbitrary callback into an Rx Observable, here's how I did it (in Coffeescript):

    this.createRxCallback = () ->
      subj = new Rx.Subject()
      subj.callback = (params...) -> subj.OnNext(params)
      return subj
    

    Then you can take any function that requires a callback, like this example from Socket.io:

    socket = new io.Socket {node_server_url}
    socket.connect()
    
    myCoolObservable = createRxCallback()
    socket.on 'message', myCoolObservable.callback
    
    myCoolObservable.Subscribe (x) ->
      console.log x
    

    Or a simple example:

    clickObservable = createRxCallback()
    document.addEventListener 'myButton', clickObservable.callback, true
    
    clickObservable.Subscribe (x) ->
      console.log "Button was clicked!"