Search code examples
djangosocketsqueuefreeswitch

Django socket communication from a view


I have an application where a request comes to a Django view and needs to communication with Freeswitch over a socekt. I would like to have the socket open already as I need to auth first etc. And hold some sort of queue all views would use to push requests into this socket and get results.

Where can I initialize this kind of socket / queue ? should I use something like Celery and a message queue to solve this ?


Solution

  • it depends (I like to say so...) but seriously, to design such an architecture you need to analyze lot of the factors, mostly because you have 2 issues here:

    • speed of the response from freeswitch
    • queuing queries to the freeswitch

    If you will have one process that will handle all of the requests and zero concurrency - then no problems at all - you will keep one socket open and reopened when needed for the duration of the whole program run. But this is unreal situation.

    If you can have only one socket opened, then there must be a queue for the requests. This implies, that all your django code must be async, there can't be an option that you app is waiting for the answer, but simple check status of the tasks.

    Celery can be used here, but it is not designed to keep socket open, as there may be several worker processes spawned by celery itself - so they can't easily share a socket between them. Using threads, greenlets etc. may be helpful but still - this is not designed to keep a connection open.

    So what you will get is to write your own daemon, I suggest to write it as a management command, which can manage such a connection and accepts data from the main application. In such a case, you must implement a queue, so celery is not needed.