Sending plain text from the backend
Let’s build the first minimalistic consumer that greets a WebSocket client when it connects. Later, we will add complexity and other actions.
All the code for the example can be found at https://github.com/PacktPublishing/Building-SPAs-with-Django-and-HTML-Over-the-Wire/tree/main/chapter-3/Sending%20plain%20text:
- We create
app/simple_app/consumers.py
with the following content:# app/simple_app/consumers.py from channels.generic.websocket import WebsocketConsumer class EchoConsumer(WebsocketConsumer): def connect(self): """Event when client connects""" # Informs client of successful connection self.accept() # Send message to client self...