7

I have a Client in AngularJS where I consume multiple SSE (Server-Sent-Events) in Java from the Server Side (there are multiple endpoints in different web servers in the backend). Diagram link

I currently register a listener to each type of event coming from each SSE connection, such as:

source.addEventListener('alpha', function(e) {
  doSomething();
}, false);

The purpose is to show a notification based on these events, and with this I have a few questions:

  • How can the client know if the information has changed in the backend?
  • How to organise and filter these events? For example, by receiving simultaneously multiple events from multiple connections, how can I manage them in order to show the client an specific notification regarding an specific event?

Note: I'm not only talking only about organising an event per type, but I also need to have in mind if an event is more important than another.

So far I only think of receiving all the events, and save them in a list that I could order and filter. Is there a problem if two SSE events are fired at the same time? Do you know of an example of this?

  • Is it a good idea to make the logic for organising the events on the client side?
  • Should I create a database for these events?

Thank you,

0

How to organise and filter these events?

but I also need to have in mind if an event is more important than another.

I would imagine having one or more queues. When events come in, they get queued. How they get handled depends on your approach requirements.

You can have multiple queues, each representing priority and queue your events into the right buckets. Higher priority queues get consumed by your notification system first.

You can also have one big queue with priority events in the head position. You can either queue events and re-sort every time events come in or do some smart logic to inject events into position to avoid re-sorting.

Is it a good idea to make the logic for organising the events on the client side?

Depends on your requirements. But it does add some overhead doing it in both places.

Should I create a database for these events?

Whenever persistent storage for events comes up, I usually think downtime and on the server end. But that depends on your requirements. If your events can be shrugged off, you don't need to persist them. But if not, you'll need to persist them somewhere.

How can the client know if the information has changed in the backend?

You're using SSE. Just have the server send another event informing you of the change. You might need to update your logic to update events in the queue as well as your payload to indicate that these events are related.

Is there a problem if two SSE events are fired at the same time?

JavaScript is single-threaded. Even if two events are sent at the same time and arrive to your browser at the same time, JavaScript will only serve one event at a time.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.