Skip to content

Commit 851b218

Browse files
authored
mux clarification
1 parent 15e45e7 commit 851b218

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ You got it! What do you need?
2626
## Multiplexing / Topics / Rooms / Channels
2727
We call them "topics" but the gist is the same. All Clients always recieve `Broadcast` events, but you can `Publish` events to a specific topic, and then only clients that have `Subscribe`d to that topic will recieve the event.
2828

29+
```go
30+
stream.Subscribe("weather", myClient)
31+
stream.Publish("weather", weatherUpdateEvent)
32+
stream.Broadcast(tornadoWarningEvent)
33+
```
34+
35+
You can also just create multiple `Stream` objects much to the same effect, then only use `Broadcast`. Streams are cheap and run no background routines, so this is a valid pattern.
36+
37+
```go
38+
weatherStream := &eventstream.Stream{}
39+
lotteryStream := &eventstream.Stream{}
40+
41+
weatherStream.Register(clientPlanningHikes)
42+
lotteryStream.Register(soonToBePoorClient)
43+
44+
lotteryStream.Broadcast(everyoneLosesEvent)
45+
```
46+
2947
### Auto subscribe certain routes to topics
3048
`Stream` implements an `http.Handler` but by default just registers clients for broadcasts.
3149

0 commit comments

Comments
 (0)