Search code examples
pythondjangowebsockettornado

Django, websockets, Tornado, how to make them communicate?


So I read that the best option at the moment to implement websockets with Django is to use Tornado to run a websocket server.

Ok I get that.

But suppose I want to broadcast messages through the websocket server whenever something happens in my django app, for example: a device of the network suddenly goes offline, I want the network administrator to get notified istantly.

The natural process I can think of is to register a signal to the device model and whenever something interesting happen I must be able to tell Tornado: hey buddy, send a message in broadcast to all the admins connected.

But how would I do that?

Maybe with some kind of publish/subscribe mechanism? Celery?

Are there any articles about this stuff? It's pretty interesting but could not have found any real use cases explained properly yet.

Thanks!

Edit: a working prototype

After following the suggestion in the accepted answer i've come up with this: https://github.com/ninuxorg/nodeshot/commit/26373a3478d77a8cc8ac8bbd0c1c45694c26a779


Solution

  • The canonical method to communicate between processes would be a queue (or a pipe). I am not sure the queues in the multiprocessing package would work. If that does not work you can create a named pipe on the file system and communicate via the pipe using os.mkfifo.