Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple way to integrate websockets to Django Rest Framework application?

My game app uses Vue frontend and Django backend with DRF. Several clients connect to same game session. Sometimes some user does something which has to be reflected also for other users. For that purpose server has to have means to request client to refresh data.

Django application is deployed on Elastic beanstalk. Typical game session has 10 players, update request is sent once in 15 seconds to the players and I don't believe we will for the foreseeable future have more than 50 simultaneous games, so the performance requirement is low.

I'm looking for a simple solution to push the update request message to the clients.

My current solution uses Redis server running on ec2. When on Django views something is changed, Django publishes update request to Redis:

r = redis.Redis(host=settings.REDIS_IP, port=6379, db=0)
r.publish(game_identifier, type)

"Type" contains information which kind of data should be updated.

In addition I've implemented a simple Node application. All clients connect to this app using Websocket. The app also subscribers to Redis, and whenever there is a notification it's emitted to subscribers using their game's identifier as channel. After this the clients refresh their state with REST. Node application is also run on Elastic Beanstalk.

Everything works but having one Elastic Beanstalk application and one ec2 instance just for sending simple notifications to clients seems like an overkill. I would love to hear ideas how to build this simple notification with less moving parts. If the backend was built using Node I could do this with just a few lines of code.

Some possibilities / questions:

  • Replace Redis instance with Elasticache?
  • Replace Node application with Django Channels?
  • Is there a way to cope totally without Redis / Elasticache? Disable Channels layers? (I'm not too familiar with Channels yet.)
  • Something else?
like image 420
tputkonen Avatar asked Mar 04 '23 05:03

tputkonen


1 Answers

You may be better of leaving the notification server on its own for modularity and scalability and reusability. But if you insist on going with a single server, you could use Django channels in place of the Node server and the Redis instance as the Channel layer so you can have it in the same code base as the DRF app. There are already a couple of libs integrating DRF and Channels out there. I use this one DjangoChannelsRestFramework

like image 186
Ken4scholars Avatar answered Mar 05 '23 19:03

Ken4scholars