简体   繁体   中英

Send websocket request from Django

Here is my deal:

  • I have a websocket pubsub server written in python with autobahn websocket (based on twisted).
  • I have simple website that is connected to the server and subscribed to one channel
  • Next to this I have simple Django project with one app
  • And at last I have opened terminal with curl get request that hits my Django app

After request form curl hits my django app and my view I need to send message to the server to the channel that my website is subscribed on. Basically I need websocket client that can send message to specific channel and can be run in django view (no infinite blocking loop).

I've tried to figure out this over 4 days. I've wrote to writ autobahn pubsub client with twisted.reacotr, but I can send the message only once (after I stop reactor to finish django request I cannot start it again, because it's not restartable).

I've olso tried djagno-socketio, but there is but, so I cannot see clients when I try to send the message from django view.

ws4py doesn't support channels

Thanks for any advice. Great would be an example.

You should be able to run Django and Autobahn within 1 server:

Here is an example that shows how to use Flask/WSGI with Autobahn. And Django can run in a WSGI container as well .

Also it should be possible to run an Autobahn-based WebSocket client from a Django/WSGI/Twisted based server using above method as well.

The point is: Twisted can act as a WSGI container and will then run the WSGI Web app (which can be blocking) on a background pool of worker threads. This will allow you to use the asynch features of Twisted, and eg run Autobahn - either client or server. So you might also rethink the overall architecture: why run the Autobahn server as a different process at all?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM