Click here to Skip to main content
16,016,580 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a Django website and I have a real-time "game" on it. Multiple people connect and play it.

Every X seconds the users in the specific game will be getting updates. I have done this using WebSockets and it works, I would say, pretty good. The things I think that using WebSocket-s is an overkill, plus I'm not utilising the Bi-directional part of it, rather using it to get the updates from the server and using AJAX to send the data.

I'm considering switching over to Server Sent Events. I've used it a bit and read about it online and have decided that it would be a good fit for my project (using it instead of WS).

The "problem" that I have is that I'm not sure how to (I assume its possible) use something like a groups in ws. I've searched online and the best solution that I could find is doing the client side filtering, which although its totally fine, I do not prefer as I think it's a waste of resources.

The way that I have it set up is that based on the url for the game, the users that are connecting would be placed in that specific group (I'm using channels and redis) and only receive data for that group, and I would like to keep it like that.

With a simple function: send_event('test', 'message', {'data': 'text'}) I'm able to send the data to the specific channel (group: test).

The setup for it looks like this:

Python
'http': URLRouter([
        url(r'^events/', AuthMiddlewareStack(
            URLRouter(django_eventstream.routing.urlpatterns)
        ), { 'channels': ['test'] }),
        url(r'', get_asgi_application()),
    ])


I have read the code for it, but I'm not sure what I can do or maybe how could I change the given name of `test` in this example to be specific (different for each game).

As I said I could still go and do client side filtering and it's fine but I would really not prefer it.

Just for clarification, I know that I can set multiple pre defined urls and have them assigned each to a different channel name, but what I'm looking for is way to apply the specific url of the game that the user is in.

What I have tried:

I use django channels in order to receive data from the server, but can't figure out how to set up the grouping mechanism with Server Sent Events
Posted
Updated 28-Feb-22 13:07pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900