Reactive-Extensions / RxJS

The Reactive Extensions for JavaScript
http://reactivex.io
Other
19.49k stars 2.1k forks source link

Observable for long polling #1118

Open Roaders opened 8 years ago

Roaders commented 8 years ago

Hi All

I want to write a Nodejs / browser app that returns data from my node server and data from a third party api. I want to immediately return the data stored on my node server to the browser and leave the connection to the browser open. At the same time I want to send a request to the third party server to get the latest information. When that call returns to my node server I want to send the latest information to my browser app and close the connection.

In my browser app how would I implement this as an observable? Usually I would do this with fromPromise but then I would get all the data in one lump. I think that I might need to use a Subject and use XmlHttpRequest and get the data myself and then call onNext / onComplete but that all seems a bit long winded, I assume that this has been solved already.

This information will be an array or json objects so I expect to send a list of say 20 object initially then send another 2 or 3 later. If I am implementing this myself presumably when I get more data I am going to have to work out if the data I have is a complete json object or just a fragment before pushing it to the observable and that seems to involve a lot of string parsing and logic that I don't want to get into...

Thanks, I hope someone can help. (sorry if this isn't the correct place to ask this sort of thing. Please delete if not)

whiteinge commented 8 years ago

Server sent events (SSE) sounds like it will fit the bill nicely here. It's long-polling. The spec is crazy small and simple, works well with firewalls and proxies, it has decent browser support, and RxJS DOM has an observable wrapper for it.

Add an SSE stream on your Node server that emits individual JSON objects of your own data plus the result of the background requests you're making to the third party server. Use the EventStream observable in RxJS DOM in the browser to consume the stream.

The browser-side portion is a one-liner. If you get stuck on the server-side portion, I'd be happy to help with a gist later (on my phone ATM).

whiteinge commented 8 years ago

@Roaders I extracted some recent related work into a gist. Hope it's helpful:

https://gist.github.com/whiteinge/997d2be18f51637340dc

Roaders commented 8 years ago

Many thanks, I won't be able to look at it until Monday but thanks for creating the gist for me.