Skip to content

A simple implementation of a Pub/Sub Event Bus

License

Notifications You must be signed in to change notification settings

theterminalguy/whisper

 
 

Repository files navigation

whisper

Simple implementation of an Event Bus using the Publish/Subscribe pattern. Whisper comes with default implementations for Google Pub/Sub and Redis Pub/Sub. It also provides a simple interface for implementing your own Pub/Sub for example using Kafka.

Qucik Overview

Whisper's event bus allows publish/subscribe-style communication between your microservices without requiring the components to explicitly be aware of each other, as shown in the following diagram:

event-pubsub

Source: .NET Microservices

A trimmed down version of the above diagram is shown below:

pubsub-basic

Source: .NET Microservices

Installation

go get github.com/10hourlabs/whisper

Usage

Google Pub/Sub

package main

import (
    "context"
    "fmt"
    "log"
    "time"

    "github.com/10hourlabs/whisper"
    "github.com/10hourlabs/whisper/google"
)

type HelloWorldEvent struct {}

type HelloWorldPayload struct {
	CreatedAt int64  `json:"created_at"`
	UpdatedAt int64  `json:"updated_at"`
	Message   string `json:"message"`
}

func (h *HelloWorldEvent) GetContext() context.Context {
	return context.Background()
}

func (h *HelloWorldEvent) GetEventName() event.Event {
	return "hello-world"
}

func (h *HelloWorldEvent) GetSubscriptionID() string {
	return "hello-world-subscription"
}

func (h *HelloWorldEvent) ValidatePayload(payload []byte) error {
	var p HelloWorldPayload
	if err := json.Unmarshal(payload, &p); err != nil {
		return whisper.ErrInvalidPayload
	}
	return nil
}

func (*HelloWorldEvent) Handle(ctx context.Context, body []byte) error {
	var data HelloWorldPayload
	json.Unmarshal(body, &data) // gauranteed to not error
	fmt.Printf("%s\r ", data.Message)
	return nil
}

func main() {
    conf := whisper.NewConfig(context.Background(), "connection-name")
    conf.RegisterEvents([]whisper.EventHandler{
        &HelloWorldEvent{},
    })
    go func() {
        if err := whisper.Listen(whisper.NewGooglePubSub(), conf) ; err != nil {
            log.Fatalf("failed to subscribe: %v", err)
        }       
    }()
    whisper.Wait(time.Second * 5)
}

About

A simple implementation of a Pub/Sub Event Bus

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%