Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django channels and socket.io-client

I'm trying to use them for the first time and wonder I'm headed to the right direction.

Here are my understandings,

socket.io is a wrapper around websocket, and falls back to sub-optimal solutions when websocket is not available.

Django channels can talk websocket as well.
(I think it converts django as a message queue like system. although this understanding or misunderstanding should affect this question)

So I'm trying to use Django channels on the server and socket.io-client on the client.

socket.io has api which looks like

socket.on(type, (payload)=> {})

Whereas Django channels has a form of

message.reply_channel.send({ "text": json })

is the "text" type of socket.on(type)?

Can Django channels and socket.io-client talk to each other?

like image 830
eugene Avatar asked Oct 20 '16 08:10

eugene


People also ask

Can you use Socket.IO with django?

Django with SocketIO Create a socket app in Django project and add it to INSTALLED_APPS . Now inside socketio_app , edit views.py and add following code. Download views.py file from here. This code will create socket events for your applications.

What is the difference between Socket.IO and socket IO client?

socket-io. client is the code for the client-side implementation of socket.io. That code may be used either by a browser client or by a server process that is initiating a socket.io connection to some other server (thus playing the client-side role in a socket.io connection).

What are django channels?

Django Channels facilitates support of WebSockets in Django in a manner similar to traditional HTTP views. It wraps Django's native asynchronous view support, allowing Django projects to handle not only HTTP, but also protocols that require long-running connections, such as WebSockets, MQTT, chatbots, etc.


2 Answers

From the Socket.IO README:

Note: Socket.IO is not a WebSocket implementation. Although Socket.IO indeed uses WebSocket as a transport when possible, it adds some metadata to each packet: the packet type, the namespace and the ack id when a message acknowledgement is needed. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a WebSocket server (like ws://echo.websocket.org) either. Please see the protocol specification here.

So, you shouldn't expect Channels to work directly with Socket.IO. Global browser support for websockets is at 93%, which is probably high enough to just use the websocket API directly.

like image 95
Chris Lawlor Avatar answered Nov 13 '22 23:11

Chris Lawlor


To quote the creator of django channels: https://github.com/django/channels/issues/1038

Channels doesn't support socket.io - it's a different protocol that isn't websockets or HTTP but layers on top of them. You'll have to use a socket.io server if you want to use it.

like image 21
Griffith Rees Avatar answered Nov 14 '22 01:11

Griffith Rees