Update the DOM on websocket event #3734
-
Hi, I'm using socket-io in python client. https://python-socketio.readthedocs.io/en/latest/client.html#installation (python implementation of Socket-io) # some other imports
from textual.widgets import ListView, ListItem
from textual.widget import Widget
from textual import work
import socketio
websocket = socketio.SimpleClient()
websocket.connect("http://localhost:3000")
class MessageAreaWidget(Widget):
list_view = reactive(ListView(*[], id="messages-list-view"), always_update=True)
default_messages = ["Welcome Back", "How are you"]
def compose(self) -> ComposeResult:
yield Container(self.list_view, id="messages-container")
for message in self.default_messages:
self.list_view.append(
ListItem(Static(message), class="list-item")
)
class MyScreen(Screen):
def compose(self) -> ComposeResult:
yield MessageAreaWidget()
self.websocket_receive()
@work(exclusive=True, thread=True)
def websocket_receive(self):
while True:
event = websocket.receive()
# event[0] is event name
# event[1] is message
if event[0] == "newmessage":
self.query_one(MessageAreaWidget).list_view.append(
ListItem(Static(event[1]), classes="list-item")
) As you can see, I'm trying to append new message into
Everything works fine except whenever I receive event message. Thanks you |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
See again the worker guide advice on thread safety and updating your application. |
Beta Was this translation helpful? Give feedback.
See again the worker guide advice on thread safety and updating your application.