Streams

Integrate async streams with actors for continuous data processing.

Overview

Actors can process items from async streams using the StreamHandler trait. This is useful for:

  • Processing TCP/WebSocket connections
  • Handling message queues
  • Reading file streams
  • Processing channels from other tasks

The StreamHandler Trait

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Adding Streams to Actors

Use ctx.add_stream() to attach a stream:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

TCP Connection Example

Handle incoming TCP lines as a stream:

tcp_handler.rs
rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

Channel Streams

Process items from a tokio channel:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

Multiple Streams

An actor can handle multiple streams of different types:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

Stream items are processed interleaved with regular messages. The actor maintains its sequential message processing guarantee.

Next Steps