Skip to content

A small package which implements publisher - subscriber pattern in pure Golang.

License

Notifications You must be signed in to change notification settings

ArunMurugan78/pubsub

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🚀 PubSub

GoDoc Go

A small package which implements publisher - subscriber pattern in pure Golang.

Installation

go get github.com/arunmurugan78/pubsub

Quick start

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

About

A small package which implements publisher - subscriber pattern in pure Golang.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages