Issue
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 :)
Solution
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
Answered By - Paul Tuckett
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.