Skip to content

Commit 6d83ba7

Browse files
committed
Update README.md
1 parent ade2dbc commit 6d83ba7

File tree

1 file changed

+91
-1
lines changed

1 file changed

+91
-1
lines changed

README.md

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,99 @@
1-
# Topic based Subscription Feed
1+
# Topic based Fan-Out Subscriptions
22

33
[![Go](https://github.com/janos/feed/workflows/Go/badge.svg)](https://github.com/janos/feed/actions)
44
[![PkgGoDev](https://pkg.go.dev/badge/resenje.org/feed)](https://pkg.go.dev/resenje.org/feed)
55
[![NewReleases](https://newreleases.io/badge.svg)](https://newreleases.io/github/janos/feed)
66

7+
This Go module provides fan-out synchronization methods using topic based dynamic subscriptions. Two types are available: Feed and Trigger.
8+
9+
## Feed
10+
11+
Feed is sending the same message to all subscribers. All messages are sent with preserved order and the delivery is guaranteed.
12+
13+
Example usage where topics are strings and messages some of a defined struct type.
14+
15+
```go
16+
// define feed message type
17+
type Message struct {
18+
ID int
19+
Text string
20+
}
21+
22+
f := feed.NewFeed[string, Message]()
23+
defer f.Close()
24+
25+
// subscribe to the feed topic "info"
26+
subscription1, cancel1 := f.Subscribe("info")
27+
defer cancel1()
28+
29+
go func() {
30+
for m := range subscription1 {
31+
fmt.Println("subscription 1 got message", m)
32+
}
33+
}()
34+
35+
// subscribe to the feed topic "info", again
36+
subscription2, cancel2 := f.Subscribe("info")
37+
defer cancel2()
38+
39+
go func() {
40+
for m := range subscription2 {
41+
fmt.Println("subscription 2 got message", m)
42+
}
43+
}()
44+
45+
46+
// send some messages to both subscriptions
47+
f.Send("info", Message{
48+
ID: 25,
49+
Text: "Hello, there.",
50+
})
51+
52+
f.Send("info", Message{
53+
ID: 101,
54+
Text: "Something happened",
55+
})
56+
57+
f.Send("info", Message{
58+
ID: 106,
59+
Text: "Something else happened",
60+
})
61+
```
62+
63+
## Trigger
64+
65+
Trigger is notifying subscribers if an event happened. If multiple events have happened, only one trigger event will be received by subscribers. This approach is ensuring that no unnecessary repetition of events happens and that appropriate action is done once after the event is received.
66+
67+
68+
```go
69+
t := feed.NewTrigger[int]()
70+
71+
// subscribe to topic 100
72+
subscription1, cancel1 := t.Subscribe(100)
73+
defer cancel1()
74+
75+
go func() {
76+
for range subscription1 {
77+
// do something
78+
}
79+
}()
80+
81+
// subscribe to topic 100
82+
subscription2, cancel2 := t.Subscribe(100)
83+
defer cancel2()
84+
85+
go func() {
86+
for range subscription2 {
87+
// do something
88+
}
89+
}()
90+
91+
// send a few events to the topic 1
92+
t.Trigger(100)
93+
t.Trigger(100)
94+
t.Trigger(100)
95+
```
96+
797
## Installation
898

999
Run `go get -u resenje.org/feed` from command line.

0 commit comments

Comments
 (0)