Django Channels Realtime API

Viewed 3041

So, I am building an API with Django REST Framework for my React Native App. Now I want to implement a chat feature and I wanted to take Django Channels to build a realtime chat feature over a TCP Connection. Now I read I couldn't build a Realtime API with Django Channels. Does it mean, I can't use Django Channels on my Server and this in my React Native APP?:

var ws = new WebSocket('ws://host.com/path');

ws.onopen = () => {
  // connection opened
  ws.send('something'); // send a message
};

ws.onmessage = (e) => {
  // a message was received
  console.log(e.data);
};

ws.onerror = (e) => {
  // an error occurred
  console.log(e.message);
};

ws.onclose = (e) => {
  // connection closed
  console.log(e.code, e.reason);
};

I did unterstand it, like another version off a HTTP(S) connection or a kind of :)

2 Answers

I'm using Django Rest Framework with Angular and I am able to harness the power of both. You just add your channels logic into your project. Then when you deploy your app, you'll proxy pass your websocket requests through Daphne, an asynchronous protocol server, then proxy pass your http requests through an http protocol server like Gunicorn. Check out my example on github: https://github.com/ptuckett86/channel_chat

Answer


Django REST Framework is a framework to make things less hard. You also could build an API with pure Django but it isn't easy. So to answere the question:

  • Python Django REST Framework makes things less hard
  • You could build a REST API with pure Django
  • You can build a HTTP or TCP API with Django

Information

Pure Django Django Rest Framework Django Channels
Framework for building your backend. You can also build a REST Framework with pure Django, but it isn't that easy The REST Framework makes things easier. You can easily build a nice REST API for your App or Website Django Channels helps to build a Real-time API with Django -> It brings WebSockets to Django. So you haven't fetch data, instead, Django pushes them into your APP or Website (e.g. a chat)
Can build a Rest API Can build a Rest API Can't build a Rest API
Can't build a Websocket API Can't build a Websocket API Can build a Websocket API

Links

Related