Skip to content

Instantly share code, notes, and snippets.

@AmirSoleimani
Last active January 10, 2023 08:18
Show Gist options
  • Save AmirSoleimani/97298c6a94d83d3672765fb31c23194a to your computer and use it in GitHub Desktop.
Save AmirSoleimani/97298c6a94d83d3672765fb31c23194a to your computer and use it in GitHub Desktop.
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
Copy link
Author

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

@baxiry
Copy link

baxiry commented Jun 1, 2022

so informative

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