https://github.com/aruuunn/pubsub
A small package which implements publisher - subscriber pattern in pure Golang.
https://github.com/aruuunn/pubsub
golang publish-subscribe pubsub
Last synced: 3 months ago
JSON representation
A small package which implements publisher - subscriber pattern in pure Golang.
- Host: GitHub
- URL: https://github.com/aruuunn/pubsub
- Owner: aruuunn
- License: mit
- Created: 2021-07-12T14:32:42.000Z (almost 5 years ago)
- Default Branch: main
- Last Pushed: 2021-07-13T12:14:15.000Z (almost 5 years ago)
- Last Synced: 2026-01-14T17:48:54.086Z (5 months ago)
- Topics: golang, publish-subscribe, pubsub
- Language: Go
- Homepage: https://godoc.org/github.com/arunmurugan78/pubsub
- Size: 18.6 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# 🚀 PubSub
[](https://godoc.org/github.com/arunmurugan78/pubsub)
[](https://github.com/ArunMurugan78/pubsub/actions/workflows/go.yml)
A small package which implements publisher - subscriber pattern in pure Golang.
## Installation
```bash
go get github.com/arunmurugan78/pubsub
```
## Quick start
```go
package main
import (
"context"
"fmt"
"github.com/arunmurugan78/pubsub"
"sync"
)
func main() {
var wg sync.WaitGroup
ctx := context.Background()
//Create a new PubSub instance
pbsb := pubsub.New(ctx)
channelNames := []string{"channelOne", "channelTwo"}
//Subscribe to required "channels"
sub := pbsb.NewSubscription(channelNames)
defer sub.UnSubscribe()
//Will get the published data through this go channel
channel := sub.Channel()
wg.Add(1)
go func() {
defer wg.Done()
select {
case message := <-channel:
// Received Data will be of type pubsub.Message
fmt.Println("Received ", message.Value, " from ", message.ChannelName)
//Output "Received 100 from channelTwo"
case <-ctx.Done():
return
}
}()
//Publish data to the specified channels
//Here we are publishing value 100 to channelTwo
pbsb.Publish([]string{"channelTwo"}, 100)
wg.Wait()
}
```