or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

channel-communication.mdcore-framework.mdevents-interactivity.mdexception-handling.mdfile-management.mdhtml-system.mdindex.mdresponse-system.mdrouting-urls.mdstatic-files-assets.md

channel-communication.mddocs/

0

# Channel Communication

1

2

Pub/sub messaging system for communication between views, background tasks, and external services. Supports topic-based messaging with message expiry and local/broadcast modes.

3

4

## Capabilities

5

6

### Channel Class

7

8

Core messaging channel for pub/sub communication.

9

10

```python { .api }

11

class Channel:

12

def __init__(self, topic: str):

13

"""

14

Create a messaging channel.

15

16

Args:

17

topic (str): Channel topic name

18

"""

19

20

def subscribe(self, handler):

21

"""

22

Subscribe to channel messages.

23

24

Args:

25

handler: Function to handle incoming messages

26

"""

27

28

def unsubscribe(self):

29

"""Unsubscribe from channel messages."""

30

31

def send(self, message_data=None, expiry=None, local: bool = False):

32

"""

33

Send message to channel.

34

35

Args:

36

message_data: Data to send

37

expiry: Message expiry time

38

local (bool): Whether message is local only

39

"""

40

```

41

42

### Message Class

43

44

Represents a channel message.

45

46

```python { .api }

47

class Message:

48

topic: str # Channel topic

49

data: object # Message data

50

timestamp: float # Message timestamp

51

expiry: float # Message expiry time

52

local: bool # Local message flag

53

```

54

55

#### Usage Example

56

57

```python

58

from lona import App, View, Channel

59

from lona.html import HTML, H1, Button, P

60

61

app = App(__file__)

62

63

# Create a global channel

64

notifications = Channel('notifications')

65

66

@app.route('/sender')

67

class SenderView(View):

68

def handle_request(self, request):

69

send_button = Button('Send Notification')

70

html = HTML(H1('Sender'), send_button)

71

self.show(html)

72

73

while True:

74

self.await_click(send_button)

75

notifications.send({

76

'message': 'Hello from sender!',

77

'timestamp': time.time()

78

})

79

80

@app.route('/receiver')

81

class ReceiverView(View):

82

def handle_request(self, request):

83

message_display = P('Waiting for messages...')

84

html = HTML(H1('Receiver'), message_display)

85

86

def handle_notification(message):

87

message_display.set_text(f"Received: {message.data['message']}")

88

89

notifications.subscribe(handle_notification)

90

self.show(html)

91

```

92

93

## Types

94

95

```python { .api }

96

from typing import Union, Optional, Dict, Any, Callable

97

98

MessageData = Union[Dict[str, Any], str, int, float, bool, None]

99

MessageHandler = Callable[['Message'], None]

100

TopicName = str

101

```