Django-channels consumer.py to display a table.py from django-table2 in a template in Django

Fundamentally, there’s not a whole lot of difference between the two.

If you think about what the chat example is doing on the client side, it’s receiving some text being sent to it, and updating the current page. In this case, it’s adding the text to a div.

If you’re rendering a complete table, and you send the html for that table through the connection, then the client will add that text to a div.

So there’s no functional difference in the client for the basic case.

Now, when you want to have different things being updated, you need to have your JS examine the data being returned to determine what should be done, but that’s not a significant conceptual difference.

On the server side, again - there’s no conceptual difference here. In the chat example, the consumer receives text, and turns around and sends that text back out. In your tables example, the consumer would receive whatever message it needs to receive, render the html for the table, and send that back out to the client.

So the infrastructure doesn’t change. You’re adding functionality to the chat app, not replacing it.

Side note: I strongly encourage you to take a look at HTMX for handling websockets. It was a game-changer for me. There are so many things it makes so much easier.

1 Like