Using Django REST framework for real-time chat? - django

I'm trying create a real-time chat app with Django and Vuejs. I would like to use django-rest-framework for this but I don't know how to make it real-time. I have used django-channels before but I'm curious if I can use only django-rest-framework as an alternative or maybe integrated it with django-channels.

You don't have to integrate them, because django channels are sending and accepting json data to websocket, as your rest framework api does. Rest framework is made for api, django channels for websockets, they won't have nothing in common cuz they have different implementations. The only thing you could consider integrating is rest framework auth inside django channels see: How do you authenticate a websocket with token authentication on django channels?

I also am a beginner but from what I know.. rest framework isn't suitable for "real-time" working of web-application cause it can't make a 2-way connection (like.. send data to other memebers in group without them making a call to it) for this purpose django channels is a better medthod cause its making a websocket connection.
do let me know if i m wrong.

Related

How to implement Angular with Django

I have requirement for creating an application using Angular(TypeScript) as frontend and Django as Backend, where I will be working only on Django (backend). Is that creating an REST API only enough to communicate with front end. Is that only possible in the backend in this case?
Normally while working in Django I used to work in forms, views and will renders in the html and create API for it but in this case where I have no idea how angular would work even. Just creating an API only sufficient when communicating with Angular
Yes, creating API's using Django/DRF is sufficient to communicate with an Angular app.
Below is an illustration of a basic architecture of a Django Angular application.
API endpoints in Django will be served via the urls.py files using the DRF (Django Rest Framework. The angular client will send HTTP requests using the Http Client Module and display the retrieved data on the components/pages. A data service created in Angular will use the HTTP client to send and get data. Angular Router will handle navigations to pages and components.
Yes, you will be using Django for REST API views. Forget about templates and Django forms, you will be just sending and receiving JSONs.
If you want to host it together you will be probably serving just index.htm. The tricky part is that you need match all possible app routes to index too (the whole Angular lives there, handling routing on client side. But initial request after reload still can lead to arbitrary url)
More common way is using two separate containers, one for api and one for angular app. Then you can provide server side rendering on angular app (this will be still calling api from it) and separate api without need to handle index.

How create application backend in django and create a api so that other developers can use that as well

I just started learning Django and wanted to make an application with API (probably REST) endpoints so that other developers can use it as well (like GitHub API), and I could connect it to my react app and android app. I want my backend to be on a different server and do not want to build my react app integrated with Django backend using the REST Framework.
Can anyone just give me the general idea of what I need to do?
P.S. Sorry in advance if my question sounds silly, I am still new to backend.
Creating a Django REST API should be your first step. This is pretty simple if you already have a Django app running, and will allow your React app or Android app to connect and access data.
Authentication is a step beyond. You may want to look into implementing authentication on the server you will be hosting your back end on. You can also read through this article for some guidance on implementing auth on the DRF side.
Best of luck!
First off all you can start from this.
https://www.django-rest-framework.org/tutorial/quickstart/

How to implement websocket for push message using django-rest-framework as backend and angular2 as frontend?

I want to implement websocket to send push notification to client.
I am using django-rest framework as backend and angular2 as frontend.
I know that django support HTTP protocol only. And I am unable to get
any such links,blogs or resource which helps me to achieve websocket
completely.
Currently I am using polling from frontend.
There are some third party apps which I found and may be useful for
implementing push message. They are...
pywebsocket
tornado
Django Channels
I don't think showing some code is significant here because i have no
code relevent to implementation of websocket.
So can you people suggest me the best way to implement this thing.Any
link,any blog or any code which may help.
One possible solution is to deploy a separate Tornado app which communicates with Front-end using WebSocket. Then, whenever the Django Back-end wants to send a push notification to the Front-end, it asks the Tornado app and Tornado app delivers the push notification to Front-end.
I have described the process in slightly more detail in this answer of mine. You might want to have a look.

What is the role of django REST framework if I want to integrate angularjs with django?

I am trying to set up django with angularjs and play around with it. Most of the tutorials contain something about django REST framework, Creating endpoints for an application, APIs and so forth. What is django REST framework, what are endpoints, APIs, why are they important for angular?
First of all, you should learn what is angularJS and how it works, with that knowledge is pretty clear why there is need for an API.
In short words: You feed angularJS with template and data, AngularJS will take it and build correct site on client side. There won't be any benefits if you won't change data dynamically. That's why you need to have API for you app (to exchange data, not pre-build templates) and for that API, good choice is Django Rest Framework.

emitting Signal from server to the clients in Python Django like SignalR

I'm using Backbone.js and Python Django Combo.
For checking user is authenticated or not, I'm using setTime out method and call a method which make ajax call to the server.
I heard SignalR from my friend who is interested in .Net Technologies. This can emiting signal from server to client. So he say there is no need to poll periodically with signalR.
Any help or idea will be appreciated.
Possible data flow diagram:
.
If you want to control the data that is pushed to your web clients from your Django app, you will need to use SignalR as a relay of sorts which can be hosted with an ASP.NET app.
The ASP.NET app can have REST endpoints accessible only to your Django app, and can then from there based on the REST parameters push messages to some or all of your clients. Example of doing this with ASP.NET MVC.
The SignalR Wiki can be a good resource for this. You will also need to EnableCrossDomain for this setup to work.
If you don't like the idea of setting up another server just to push data to your clients you might prefer a cloud-based offering like Pusher with a prebuilt wrapper around their REST API.
If you want to use Python to actually push to the clients you can use something like tornado.websocket, but that won't support browsers that don't support the final WebSocket spec.