# How do I integrate python-socketio with django?

**URL:** <https://forum.djangoproject.com/t/how-do-i-integrate-python-socketio-with-django/37546>\
**Category:** Forms & APIs\
**Created:** [December 29, 2024, 4:24pm UTC](https://forum.djangoproject.com/t/how-do-i-integrate-python-socketio-with-django/37546 "2024-12-29T16:24:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ohnoimded](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ohnoimded/32/25979_2.png) [@Ohnoimded](https://forum.djangoproject.com/u/Ohnoimded)\
**Post date:** [December 29, 2024, 4:24pm UTC](https://forum.djangoproject.com/t/how-do-i-integrate-python-socketio-with-django/37546/1 "2024-12-29T16:24:52Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 29, 2024, 5:31pm UTC](https://forum.djangoproject.com/t/how-do-i-integrate-python-socketio-with-django/37546/2 "2024-12-29T17:31:36Z")

</div>

Welcome @Ohnoimded !

> [@Ohnoimded](#):
>
> How do I ensure both socketio and django share the same configs

You don’t - at least not directly.

The [socket.io](http://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](http://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](https://github.com/django/channels/issues/1038)

---

<div class="post-metadata">

**Author:** ![Ohnoimded](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ohnoimded/32/25979_2.png) [@Ohnoimded](https://forum.djangoproject.com/u/Ohnoimded)\
**Post date:** [December 29, 2024, 5:48pm UTC](https://forum.djangoproject.com/t/how-do-i-integrate-python-socketio-with-django/37546/3 "2024-12-29T17:48:56Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 29, 2024, 6:05pm UTC](https://forum.djangoproject.com/t/how-do-i-integrate-python-socketio-with-django/37546/4 "2024-12-29T18:05:29Z")

</div>

> [@Ohnoimded](#):
>
> So is it better to implement it as a different service/server and avoid mixing django and python-socketio’s AsyncServer?

“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.

---

<div class="post-metadata">

**Author:** ![Ohnoimded](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ohnoimded/32/25979_2.png) [@Ohnoimded](https://forum.djangoproject.com/u/Ohnoimded)\
**Post date:** [December 29, 2024, 6:29pm UTC](https://forum.djangoproject.com/t/how-do-i-integrate-python-socketio-with-django/37546/5 "2024-12-29T18:29:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [December 29, 2024, 6:36pm UTC](https://forum.djangoproject.com/t/how-do-i-integrate-python-socketio-with-django/37546/6 "2024-12-29T18:36:06Z")

</div>

> [@Ohnoimded](#):
>
> Socketio needs to be there instead of channels mainly due to the range of features it offers out of the box.

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

> [@Ohnoimded](#):
>
> If I have to create a separate server only for handling chat, I’d have to use nodejs or flask

Why?

---

<div class="post-metadata">

**Author:** ![Ohnoimded](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/ohnoimded/32/25979_2.png) [@Ohnoimded](https://forum.djangoproject.com/u/Ohnoimded)\
**Post date:** [December 29, 2024, 7:01pm UTC](https://forum.djangoproject.com/t/how-do-i-integrate-python-socketio-with-django/37546/7 "2024-12-29T19:01:42Z")

</div>

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.
