Search code examples
pythondjangonext.jsfrontendbackend

How to sends custom status messages to the front end when the endpoint has some heavy process time, if I am using Django as backend?


this is a very general question. I was trying to look for something but here I might have a better start point. I am working with a python tool which is still on progress but the UI was done previously with PYQT5. What I wanted to do is creating some web front end (NextJs) and connecting with Django as backend and the calculations will be done on the local server we have.

What I wanted to check is how I can send real time updates to the front end about the process that is happening on the backend. lets say if there is a loop, so sending the 1/10,2/10 and so on. Is this a socket thing (?) like streaming messages while is inside the http request.

Thank you!

I am still on the looking for options stage, what I have though is that this can be possible with sockets and emit messages from the backend to the front.


Solution

  • You can use Channels to create a socket and a socket library like socket.io for NodeJS to form a connection to send/receive data.

    Then just handle them in your NextJs frontend.

    Since you are just sending and not receiving, a unidirectional method like SSE (server sent events) can be used too. However, it comes with it's own set of issues (see below).

    How can I make SSE with Python (Django)?

    I would recommend sticking with Channels or if you have the time, take a look at StreamingHttpResponse.