Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pub/Sub Pattern Golang
package main
import (
"patternsample/mq"
"fmt"
"time"
)
func main() {
// New
mque := mq.New()
// Subscribe topic
mque.Subscribe("Notify", func(m *mq.Message) {
fmt.Println("Received, Topic: Notify")
})
// Subscribe topic
mque.Subscribe("Test2", func(m *mq.Message) {
fmt.Println("Received, Topic: Test2")
})
// Publish value into topic
mque.Publish(
mq.Message{
Topic: "Notify",
Value: "blablabla",
},
)
// ;)
time.Sleep(2 * time.Second)
// Publish value into topic
mque.Publish(
mq.Message{
Topic: "Test2",
Value: "blablabla",
},
)
// Publish value into topic
mque.Publish(
mq.Message{
Topic: "NotFound",
Value: "blablabla",
},
)
// ;)
time.Sleep(1 * time.Second)
}
package mq
import "errors"
//Message struct
type Message struct {
Topic string
Value interface{}
}
//Channel struct
type Channel struct {
ch chan Message
}
//Mq struct
type Mq struct {
topics map[string]*Channel
}
// var sessions map[string][]Session
//New func
func New() *Mq {
return &Mq{
topics: map[string]*Channel{},
}
}
//Subscribe method
func (s *Mq) Subscribe(topic string, handler func(m *Message)) error {
// generate topic if not exist
if _, exist := s.topics[topic]; exist {
//TODO: make Session List for handle multiple Subscribe on Single Topic
return errors.New(("Subscribe exist, topic:" + topic))
}
s.topics[topic] = &Channel{
ch: make(chan Message),
}
go func() {
for {
c := <-s.topics[topic].ch
handler(&c)
}
}()
return nil
}
//Publish method
func (s *Mq) Publish(msg Message) error {
if _, ok := s.topics[msg.Topic]; !ok {
return errors.New("Topic has been closed")
}
s.topics[msg.Topic].ch <- msg
return nil
}
@AmirSoleimani

This comment has been minimized.

Owner

AmirSoleimani commented Nov 20, 2018

Per Subscribe Topic ~> Generate Single Channel.
You can not subscribe topic more than once

if you want to has multiple subscribe on a Topic, Create Subscribe List and publish on each session (Per Subscribe -> Channel) (FanOut)

sample text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment