MAGYS
MAGYS

Reputation: 301

Django: Send regularly updates over websocket

We are currently developing an application with django and django-omnibus (websockets). We need to send regularly updates from the server (django) to all connected clients via websockets.

The problem is, that we can't use cron or something related to do the work. I've written a manage.py command but through some limitations it seems omnibus can't send the message to the websocket if launcher by python manage.py updateclients.

I know that django is not designed for this kind of stuff but is there a way to send the updates within the running django instance itself?

Thanks for help!

Upvotes: 0

Views: 747

Answers (1)

Anentropic
Anentropic

Reputation: 33823

Is the reason you can't use cron because your hosting environment doesn't have cron? ...or because "it seems omnibus can't send the message to the websocket if launcher by python manage.py" ?

If you simply don't have cron then you need to find an alternative such as https://apscheduler.readthedocs.org/en/latest/ or Celery also provides scheduled tasks.

But if the problem is the other side: "a way to send the updates within the running django instance itself" then I would suggest a simple option is to add an HTTP API to your Django app.

For example:

# views.py
from django.core.management import call_command

def update_clients(request):
    call_command('updateclients')
    return HttpResponse(status=204)

Then on your crontab you can do something like:

curl 127.0.0.1/internalapi/update_clients

...and this way your updateclients code can run within the Django instance that has the active connection to the omnibus tornado server.

You probably want some access control on this url, either via your webserver or something like this:
https://djangosnippets.org/snippets/2095/

Upvotes: 2

Related Questions