HackSoftware / Django-Styleguide

Django styleguide used in HackSoft projects

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Socket Programming

Sajjad-Khoshdooni opened this issue Β· comments

commented

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

@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 πŸ™Œ

commented

@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?

@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!

Closing for now. Feel free to reopen πŸ™Œ