Ch1irxo 17
Ch1irxo 17

Reputation: 27

How to use websockets in drf project with react-frontend?

How to work with websockets in a split (backend/frontend) project?

In a full Django project, I did it like this (1st answer): Dynamic updates in real time to a django template

Now, the frontend is separate on React and the backend is on DRF. The frontend will be using socketio. How, in this case, should websockets be properly implemented in DRF? Should I still use channels?

I thought that it could be done similar to the answer on Stack Overflow, where now the frontend is done in React instead of Django templates. But the frontend mentioned that endpoints are not needed, just events:

For example, he will have something like socket.on("getTasks") and socket.on("addTask", task: {"new task"}). And I have no idea how to work with this

All I've tried is the way from stackoverflow.

Upvotes: 0

Views: 113

Answers (1)

mirodil
mirodil

Reputation: 537

You can simply create websocket object and use it with events.

const socket = new WebSocket("endpoint")

endpoint can be what you defined in your django-channels route file like ws://localhost:8080/whatever/

Upvotes: 0

Related Questions