HackSoftware / Django-Styleguide

Django styleguide used in HackSoft projects
MIT License
5k stars 511 forks source link

Socket Programming #131

Closed Sajjad-Khoshdooni closed 1 year ago

Sajjad-Khoshdooni commented 1 year ago

how do you handle Socket Programming and Channels and what are the best practices?

RadoRado commented 1 year ago

@Sajjad-Khoshdooni Hello :wave:

From our experience, if you can afford to run the channels instance separately from the web instance, you'll have better control over it.

Same goes with things that require persistent & long-running connections.

I can provide a better answer if you give me more context & examples :raised_hands:

Sajjad-Khoshdooni commented 1 year ago

@Sajjad-Khoshdooni Hello 👋

From our experience, if you can afford to run the channels instance separately from the web instance, you'll have better control over it.

Same goes with things that require persistent & long-running connections.

I can provide a better answer if you give me more context & examples 🙌

For example, suppose you need to connect to an external WebSocket, subscribe to topics, and retrieve data. What is your overall solution?

RadoRado commented 1 year ago

@Sajjad-Khoshdooni Sorry for responding late. We've missed the notification here.

Generally speaking, in Django context, you'll need an asgi implementation of some sorts - https://asgi.readthedocs.io/en/latest/implementations.html

Additionally, you'll want to be able to consume that websocket in an async way (looking for Python library support)

Everything else really depends on how you are going to consume the data from that socket:

  1. Is it going to be some kind of "publish request to topic X, expect response from topic Y" - for example - this is a standard way of working with MQTT.
  2. Are you going to need this socket opened for longer periods of time? If that's the case, perhaps, running this as a separate process outside of Django can make more sense.

Generally speaking, look for how to do this stuff with, for example, Daphne.

Let me know if you have more questions or additional details.

Cheers!

RadoRado commented 1 year ago

Closing for now. Feel free to reopen :raised_hands: