#Tips on using SSE?

7 messages · Page 1 of 1 (latest)

sterile frost
#

My application requires real-time events except its not necessary for the client to ever send one back, so server-sent events sound perfect for this. The issue is I'm struggling to picture how one should use them effectively. For example, the client must receive updates on the status of their orders. I emit an event using the EventEmitter2 whenever an order is updated. Is it proper use of sse to use rxjs's fromEvent to subscribe to those messages? Simple repro:

@Controller()
class AppController {
  constructor(private readonly eventEmitter: EventEmitter2) {}

  @Get()
  index(): string {
    this.eventEmitter.emit("message", { data: "Goodbye world!" });
    return "Hello world!";
  }

  @Sse("events")
  streamEvents() {
    return fromEvent(this.eventEmitter, "message");
  }
}
#

Are there any performance implications or is it nothing I need to worry about

#

Unlike this scenario where the data would specific to the client, when it's public then should I use a static observable instead of creating a new one on request?

#

lastly is the added complexity (if any, It's only complex because I don't understand how to utilize it properly yet) worth using it over something like websockets?

mystic grove
#

@sterile frost - SSE is actually simpler and less resource intensive than websockets. Your server should however be serving HTTP/2.

Have a look at this SO answer.

What you'd need to do to continue with this is create a proper service for your streams (notice the private stream array of stream objects created in the controller example. This service is what you'd need to call anywhere in your application to "inject" data into the stream (as the OP of that reply notes) and of course, if you want the reply to be user specific, you'd need to relate the stream id to the user somehow (using the user Id as the stream id maybe? I'll leave that challenge up to you 🙂 ). In other words, you shouldn't need to "swap over" to eventEmitter and back. You pass the streams around and inject data as you need to.

Unlike this scenario where the data would specific to the client, when it's public then should I use a static observable instead of creating a new one on request?

If the SSE connection is for all public pushes of data i.e. all users get the same event at the same time, then of course you don't need to identify a particular stream, but you'd still need the stream object to pass around. You could just change the id to something generic like 'public' and I think you'd need a different controller path. Or, you could just use the same controller path, but you'd need to inject the same data into all streams (so looping through the streams in the stream array, which I'm not certain is the most effective way to do it). I'm new to SSE and even working with events on the backend, so I'm guessing a bit too as I go here. 🙂

Still, I hope I could help.

north trail
#

debating whether i should go with websockets or SSE for notifications that are aimed to be sent to individual users. I found some answers on stackoverflow that you can keep a map of subjects to notifications but that it might have performance issues