Rxjs: Allow creation of Observable from EventSource (server-sent events): enhancement

Created on 24 Apr 2016  Â·  12Comments  Â·  Source: ReactiveX/rxjs

RxJS version: 5.0.0-beta.1

Additional information: This is a request for enhancement.

It would be nice to have an operator to create Observables from EventSources from server-sent events.

Is is planned for a future version of RxJS? If so in which version?

If that feature is already available, maybe a relevant section could be added to the documentation.

Most helpful comment

@mattpodwysocki Are PRs welcome for this?

All 12 comments

Where is the documenation for the "from" operator?

The following link: from-static-method just links to the top of the page...

@balteo Please consider documentation is not complete, still being evolved over time.

For fromEventSources I think it's yet migrated from https://github.com/Reactive-Extensions/RxJS-DOM/blob/master/doc/operators/fromeventsource.md, maybe considerable to place under dom as same as RxJS4.

Hi OJ Kwon,
Do you mean it _is_ migrated or it is _not yet_ migrated?
Can I use it?
Regards,
Julien.

2016-04-25 9:29 GMT+02:00 OJ Kwon [email protected]:

@balteo https://github.com/balteo Please consider documentation is not
complete, still being evolved over time.

For fromEventSources I think it's yet migrated from
https://github.com/Reactive-Extensions/RxJS-DOM/blob/master/doc/operators/fromeventsource.md,
maybe considerable to place under dom as same as RxJS4.

—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
https://github.com/ReactiveX/rxjs/issues/1644#issuecomment-214181336

yet migrated

: not yet, sorry for confusion.

No problems. I'll wait for it to be available. Any timeframe?

2016-04-25 9:38 GMT+02:00 OJ Kwon [email protected]:

yet migrated

: not yet, sorry for confusion.

—
You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
https://github.com/ReactiveX/rxjs/issues/1644#issuecomment-214186025

I am also interested in seeing this ported to 5.0. Maybe @mattpodwysocki can help us here?

+1

@zh99998 that's the whole point of the discussion: it is in the old (v4) rxjs but not in the new (v5).

@kwonoj do you think it's going to be implemented? :)

@mattpodwysocki Are PRs welcome for this?

I would think that a PR is welcome for this @mattbrunetti, go for it! If it doesn't get accepted, we can always make it a separate node package.

I don't think we want to support this in the core library right now. There are probably other implementations floating around in user land.

Basically, you'd need to do something like this:

function fromEventSource(url: string): Observable<MessageEvent> {
  return new Observable<MessageEvent>(subscriber => {
    const sse = new EventSource(url);

    sse.onmessage = e => subscriber.next(e);
    sse.onerror = e => subscriber.error(e);

   return () => {
     if (see.readyState === 1) {
        sse.close();
     }
   };
}

However, if you wanted to use the special handling EventSource has for events with an event property on them:

const sse = new EventSource(url);

fromEvent(sse, 'myeventhere').subscribe(console.log);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

LittleFox94 picture LittleFox94  Â·  3Comments

benlesh picture benlesh  Â·  3Comments

giovannicandido picture giovannicandido  Â·  4Comments

samherrmann picture samherrmann  Â·  3Comments

matthewwithanm picture matthewwithanm  Â·  4Comments