Reputation: 128
I have a web app in which a teacher creates a class and is able to upload materials and add students to the class. Students can also join a particular teacher's class and download any uploaded material.
When a teacher uploads a material to a particular class, a Server Sent Event is sent to every connected client and from the client, checks are performed to find out if this event concerns the client. Here's a code sample:
let eventSource = new EventSource(url);
eventSource.onmessage = function(event){
if(joinedClasses.indexOf(event.data.class) !== -1){
//notify the user.
}
//else just discard the message.
}
I want to know if this is the proper way of doing this or should I just use (Long) Polling or some other method. Thanks.
Upvotes: 5
Views: 4413
Reputation: 402
I've implemented using query strings as keys
. You could see a minimal example here
Upvotes: 0
Reputation: 789
Depending on your back-end structure you could try with 3 different approaches:
1) Sending the user ID as a GET parameter, this way you would have specific stream for each user.
2) The other way is to have different events on the server side and then the client would listen only to the events that are relevant for them.
3) If you don't have a database, sessions could also be used for some cases.
Upvotes: 2