Skip to main content

PubSub

If you build a chat app using Flet you need somehow to pass user messages between sessions. When a user sends a message it should be broadcasted to all other app sessions and displayed on their pages.

Flet provides a simple built-in PubSub mechanism for asynchronous communication between page sessions.

Flet PubSub allows broadcasting messages to all app sessions or sending only to specific "topic" (or "channel") subscribers.

A typical PubSub usage would be:

This is an example of a simple chat application:

import flet
from flet import Column, ElevatedButton, Page, Row, Text, TextField

def main(page: Page):
page.title = "Flet Chat"

# subscribe to broadcast messages
def on_message(msg):
messages.controls.append(Text(msg))
page.update()

page.pubsub.subscribe(on_message)

def send_click(e):
page.pubsub.send_all(f"{user.value}: {message.value}")
# clean up the form
message.value = ""
page.update()

messages = Column()
user = TextField(hint_text="Your name", width=150)
message = TextField(hint_text="Your message...", expand=True) # fill all the space
send = ElevatedButton("Send", on_click=send_click)
page.add(messages, Row(controls=[user, message, send]))

flet.app(target=main, view=flet.WEB_BROWSER)