Thanks to visit codestin.com
Credit goes to Github.com

Skip to content
/ pubsub Public

PubSub is a library that provides a simple pub/sub mechanism with multiple backend storages.

Notifications You must be signed in to change notification settings

zyy17/pubsub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🔨 PubSub

PubSub is a library that provides a simple pub/sub mechanism with multiple backend storages.

Features

  • 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.

Installation

go get -u github.com/zyy17/pubsub

Quick Start

Start etcd

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.

Start a publisher

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!")))

Create a subscriber

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)
}

Run the examples

Use the following commands to compile the publisher and subscriber(make sure you have already install go and protoc):

make examples

When 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/subscriber

🔨 TO-DO List

PubSub 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)

About

PubSub is a library that provides a simple pub/sub mechanism with multiple backend storages.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published