How do I integrate python-socketio with django?

I’m trying to have a socketio based chat API in my django project. But I am facing more problems that solutions.

  1. socketio.AsyncServer does not recognise test database form pytest and runs independently with the database from settings.py.
  2. I don’t know how to have an endpoint url like /api/chat/ instead of using /socket.io/.
  3. Testing clients does not even work properly as the clients defined in pytest file are listening to each other instead of listening from the backend server. No test client.

How do I ensure both socketio and django share the same configs and I don’t want to define a sever inside the sockets.py instead of having it be defined in django’s asgi.py. Also why does it require defining cors and other features inside ther server definition?

Welcome @Ohnoimded !

You don’t - at least not directly.

The socket.io protocol is different from http, and is a layer on top of a basic websocket. There is no fundamental interoperability between the two.

You could create a custom Channels protocol handler - I looked at doing that about 6 years ago and decided it wasn’t worth the effort. (At the time, I determined that the socket.io protocol was too ill-defined and unstable for my purposes, so I abandoned it completely. That status may have changed - I wouldn’t know.)

Also see the thread at Use Socket.IO-Client to connect channels · Issue #1038 · django/channels · GitHub

So is it better to implement it as a different service/server and avoid mixing django and python-socketio’s AsyncServer? I’ve been trying to force socketio to take the test_db from pytest thinking socketio would automatically pickup that. But it’s only considering what is inside Django settings and never the test db.

“Better” is a relative term, depending upon your precise requirements, time available for development, and staff expertise, and the answer is an opinion based on all these factors.

Right now, there’s no established method to create direct interoperability between the two.

Okay. Thanks for helping me with this and I really appreciate the prompt response.

For context:
Currently, I’m the only person developing the project’s django side as well as this feature, but we need to have this as a minor feature for users. Socketio needs to be there instead of channels mainly due to the range of features it offers out of the box. If I have to create a separate server only for handling chat, I’d have to use nodejs or flask(testing with python socketio is still a headache though). But if I have any way to use it as an app alongside django, it would reduce the workload, additional setup, maintenance, and technical overhead.

What features does it offer that make it such a preferable option for a Chat application?

Why?

It provides both websockets and long polling as a fallback if websockets fail. Automatic reconnection, simpler logic, heartbeats, multiplexing, rooms management, broadcasting, etc. No need for extra development for these features.

The reason why I said I might have to go with different frameworks is socketio is primarily designed with nodejs for the server implement. And it makes more sense with the async stuff there. Python-socketio has a Flask implementation that works super well with Flask and it’s lightweight and has inbuilt support for async stuff.

So far, I could run python-socketio with django very well except for the intended purpose, that is chat. But it has its own server that runs alongside django, has a messy way of handling endpoints, auth, cors, and other stuff that django does better. Testing with pytest always fails because socketio does not pickup the testdb and only takes the default db from settings no matter how i configure the tests or settings.