Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dowlandaiello/go-simplesub
A minimalistic pubsub messaging system built on top of libp2p with routing support.
https://github.com/dowlandaiello/go-simplesub
go go-libp2p libp2p messaging networking pub-sub pubsub simplesub
Last synced: 6 days ago
JSON representation
A minimalistic pubsub messaging system built on top of libp2p with routing support.
- Host: GitHub
- URL: https://github.com/dowlandaiello/go-simplesub
- Owner: dowlandaiello
- License: apache-2.0
- Created: 2019-05-27T16:04:52.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T21:49:55.000Z (about 2 years ago)
- Last Synced: 2024-06-21T15:16:42.519Z (8 months ago)
- Topics: go, go-libp2p, libp2p, messaging, networking, pub-sub, pubsub, simplesub
- Language: Go
- Homepage:
- Size: 97.7 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# go-simplesub
A minimalistic, yet powerful pubsub messaging system built on top of libp2p.
## Rationale
```English
Why does this repo exist?
```Simple: Libp2p's pub/sub implementation simply does not provide enough flexibility
for some use cases (those that utilize a DHT or any sort of routing in particular).## Installation
```zsh
go get github.com/dowlandaiello/go-simplesub
```## Usage
```Go
package mainimport (
"context""github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
routed "github.com/libp2p/go-libp2p/p2p/host/routed"
"github.com/dowlandaiello/go-simplesub"
inet "github.com/libp2p/go-libp2p-net"
)func main() {
ctx, cancel := context.WithCancel(context.Background()) // Initialize contextdefer cancel() // Cancel
host, err := libp2p.New(
ctx,
libp2p.NATPortMap(),
libp2p.ListenAddrStrings(
"/ip4/0.0.0.0/tcp/1111",
"/ip6/::1/tcp/1111",
),
) // Initialize hostif err != nil { // Check for errors
panic(err) // Panic
}dht, err := dht.New(ctx, host) // Initialize dht
if err != nil { // Check for errors
panic(err) // Panic
}routedHost := routed.Wrap(host, dht) // Wrap host
sub, err := simplesub.NewSimpleSub(routedHost) // Initialize sub
if err != nil { // Check for errors
panic(err) // Panic
}sub.Subscribe("test_topic", handler) // Subscribe
err = sub.Publish(ctx, "test_topic", []byte("test")) // Publish to topic
if err != nil { // Check for errors
panic(err) // Panic
}
}// handler handles a new incoming stream.
func handler(stream inet.Stream, message *simplesub.Message) {
fmt.Printf("Received message: %s", string(message.Data)) // Log received
}
```## Configuration
### Route Prefixes
By default, all simplesub routes are registered under `/`.
Should one wish to add a prefix to such a route, simply pass the `WithRoutePrefix` option function
to the simplesub constructor.Example:
```Go
sub, err := simplesub.NewSimpleSub(routedHost, simplesub.WithRoutePrefix("test_net")) // Initialize subif err != nil { // Check for errors
panic(err) // Panic
}
```