# Django channels, how to turn REST endpoint into websocket endpoint?

**URL:** <https://forum.djangoproject.com/t/django-channels-how-to-turn-rest-endpoint-into-websocket-endpoint/6765>\
**Category:** Using Django\
**Created:** [February 24, 2021, 10:03pm UTC](https://forum.djangoproject.com/t/django-channels-how-to-turn-rest-endpoint-into-websocket-endpoint/6765 "2021-02-24T22:03:26Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![devspectre](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/devspectre/32/3559_2.png) [@devspectre](https://forum.djangoproject.com/u/devspectre)\
**Post date:** [February 24, 2021, 10:03pm UTC](https://forum.djangoproject.com/t/django-channels-how-to-turn-rest-endpoint-into-websocket-endpoint/6765/1 "2021-02-24T22:03:26Z")

</div>

I currently have a REST endpoint(GET) to provide some info to clients.  
But the client is going to live stream the data as the data source is frequently updated.  
The data comes from a device and that device writes data to our backend using REST API endpoints(PUT, POST).

I want to use django channel to turn that GET endpoint into websocket endpoint so the client just need to establish websocket connection once and gets data.

I don’t have any idea how to implement this.  
That get method will read data from database and do I need to periodically(let’s say once per second) read data on that socket consumer?  
Or can I just send data when the devices call PUT, POST or PATCH methods?  
Someone who has rich experience in django channels and websocket can easily answer this question.

Any help would be appreicated.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 25, 2021, 12:39am UTC](https://forum.djangoproject.com/t/django-channels-how-to-turn-rest-endpoint-into-websocket-endpoint/6765/2 "2021-02-25T00:39:56Z")

</div>

If you want to see it as a pattern, look at the “Chat Example” as the pattern.

Your client will connect to the “chat server” and will be listening to a “chat room”. The devices will be sending messages in via POST (or PUT). Your endpoint will then be responsible for sending the updated data as a channel message, which will then send it out to all connected clients.

---

<div class="post-metadata">

**Author:** ![devspectre](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/devspectre/32/3559_2.png) [@devspectre](https://forum.djangoproject.com/u/devspectre)\
**Post date:** [February 25, 2021, 9:44am UTC](https://forum.djangoproject.com/t/django-channels-how-to-turn-rest-endpoint-into-websocket-endpoint/6765/3 "2021-02-25T09:44:25Z")

</div>

Yes, I understand basic of socket.  
But I don’t see any authentication in socket communication.  
And I am not sure how I should send data from consumer.  
Do I just need to read database every one second and send it to client?  
How can I authenticate incoming connections?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 25, 2021, 1:33pm UTC](https://forum.djangoproject.com/t/django-channels-how-to-turn-rest-endpoint-into-websocket-endpoint/6765/4 "2021-02-25T13:33:22Z")

</div>

> [@devspectre](#):
>
> But I don’t see any authentication in socket communication.

That’s because authentication happens before the socket is opened. See [Django authentication](https://channels.readthedocs.io/en/stable/topics/authentication.html#django-authentication) in the Channels docs.

---

<div class="post-metadata">

**Author:** ![devspectre](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/devspectre/32/3559_2.png) [@devspectre](https://forum.djangoproject.com/u/devspectre)\
**Post date:** [February 25, 2021, 4:06pm UTC](https://forum.djangoproject.com/t/django-channels-how-to-turn-rest-endpoint-into-websocket-endpoint/6765/5 "2021-02-25T16:06:21Z")

</div>

Yes, that AuthMiddlewareStack does something for authentication I believe.  
But anyway, anybody who knows our endpoint can connect to our consumer and get data, right?  
I am trying to figure out more thorough way to secure websocket connection

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [February 25, 2021, 4:13pm UTC](https://forum.djangoproject.com/t/django-channels-how-to-turn-rest-endpoint-into-websocket-endpoint/6765/6 "2021-02-25T16:13:07Z")

</div>

No, that is not correct. Like any other endpoint, you can require an authenticated and authorized user. The docs cover this.  
(It looks like [Andrew’s multichat example](https://github.com/andrewgodwin/channels-examples/tree/master/multichat) provides a demonstration of requiring authenticated users.)

---

<div class="post-metadata">

**Author:** ![devspectre](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/devspectre/32/3559_2.png) [@devspectre](https://forum.djangoproject.com/u/devspectre)\
**Post date:** [February 25, 2021, 7:27pm UTC](https://forum.djangoproject.com/t/django-channels-how-to-turn-rest-endpoint-into-websocket-endpoint/6765/7 "2021-02-25T19:27:21Z")

</div>

Yes, I will take a look at that.

---

<div class="post-metadata">

**Author:** ![carltongibson](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/carltongibson/32/267_2.png) [@carltongibson](https://forum.djangoproject.com/u/carltongibson)\
**Post date:** [March 2, 2021, 7:23am UTC](https://forum.djangoproject.com/t/django-channels-how-to-turn-rest-endpoint-into-websocket-endpoint/6765/8 "2021-03-02T07:23:49Z")

</div>

Hi @devspectre — also take a look at [GitHub - hishnash/djangochannelsrestframework: A Rest-framework for websockets using Django channels-v3](https://github.com/hishnash/djangochannelsrestframework) (which is linked from the Channels docs too) — it aims to give you the consumers you need to work with DRF serializers.
