PubSub is a library that provides a simple pub/sub mechanism with multiple backend storages.
- Simple: Pretty simple APIs.
- Offline Messages: The subscriber can receive the messages after restarting or crashing.
- 🔨 Multiple Storage Providers: PubSub can support multiple storage providers to store the messages.
go get -u github.com/zyy17/pubsub
For now, pubsub only supports etcd as a storage provider. We will support more storage providers in the future.
You can download the etcd and start it.
Use pubsub.StartPublisher() to start a publisher and publish the message:
publisher, _ := pubsub.StartPublisher(pubsub.DefaultPublisherOptions())
defer publisher.Stop()
// Let's publish your first message!
// Publish message to the 'pubsub' topic.
publisher.Publish(context.Background(), publisher.NewMessage("pubsub", []byte("hello world!")))Use pubsub.NewSubscriber to create the subscriber:
subscriber, _ := pubsub.NewSubscriber(pubsub.DefaultSubscriberOptions())
defer subscriber.Close()
// Subscribe the pubsub topic.
subscriber.Subscribe(context.Background(), []string{"pubsub"})
// Let's receive message from the 'pubsub' topic!
for {
message, _ := subscriber.Recv()
log.Printf("Received message: %v", message)
}Use the following commands to compile the publisher and subscriber(make sure you have already install go and protoc):
make examplesWhen the building is complete, run the publisher and subscriber in two different terminals:
# Run the publisher to publish the messages.
./bin/publisher
# Open another terminal to receive the messages.
./bin/subscriberPubSub is still in unstable development. The following features are still in progress:
- unit tests
- integration tests
- limited offline message list
- message ttl
- memory backend
- redis backend
- sql backend(sqlite,mysql,pg)