A
A
Anton B2021-04-29 14:07:51
Server-Sent Events
Anton B, 2021-04-29 14:07:51

How to work with SSE?

Hello.

The question is not about technology, the question is about the algorithm of its use.

There is a game implemented as a Single Page Application. The game has many sections: home, shop, chat, tournament, etc.

The user enters the chat so as not to miss other people's messages - once a second, a call is made to the server.
The user enters the tournament in order not to miss the appearance of an opponent - once a second, a request is made to the server.
And so it is in almost all sections.

In order not to pull the server every second, Long Polling, WebSockets and Server-Sent Events were invented a long time ago. My tasks are completely covered by SSE, but I do not understand how to use this technology correctly.

Option 1.When entering the game, the user connects to his only personal channel.

And what's next?... On the back's side, I know that such a user is now holding a channel, but what should I publish there? I don't know if the user is currently in the chat, or in the tournament, or in the settings. And if in a chat, in which room?

So, when someone writes a message to a specific chat room, I have to find all users who have access to this room and who have an active connection, and send everyone information about the new message? And if the user is currently in a tournament and this information is absolutely not interesting to him? Yes, and a selection of everyone who has access to the room and who is connected can be expensive if the project is large.

Option 2. When entering the section, the user connects to the channel of the section.

The user entered the tournament - connected to the tournament channel, left - disconnected.
I went into the chat - connected to the channel of the room, left - disconnected.

Sounds nice, but is it really expensive? After all, each visit to the section will be accompanied by an additional connection to connect to the channel.

Option 3. When entering the game, the user connects to all channels available to him.

We know that the user has 15 rooms in the chat, a tournament, an arena, notifications, in total we connect him to 18 channels.

But, here again, he is sitting in one section, why does he need information from another?
Will the server and browser allow you to keep a large number of channels?

Please share your experience.

Thanks for answers.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Shumov, 2021-04-29
@inoise

Well,
1. SSE does not fully cover your tasks. You address in both directions
2. You need to have an event system on the back and organize subscribe / unsubscribe for certain types of messages by parameters
3. in any case, you will have to store a list of subscribers and send each separately, unless you have a websocket.
And, surprisingly, but for all of the above, if, of course, you learn to read the documentation and do not have ultra-high loads, GraphQL is suitable because it has subscriptions, and commands (mutations) and just getting the necessary data (query).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question