JoinPubSub joins a pubsub topic and sets up message handling
(p2p *P2P, name string, topicName string)
| 34 | |
| 35 | // JoinPubSub joins a pubsub topic and sets up message handling |
| 36 | func JoinPubSub(p2p *P2P, name string, topicName string) (*PubSub, error) { |
| 37 | topic, err := p2p.PubSub.Join(topicName) |
| 38 | if err != nil { |
| 39 | return nil, err |
| 40 | } |
| 41 | |
| 42 | sub, err := topic.Subscribe() |
| 43 | if err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | |
| 47 | ps := &PubSub{ |
| 48 | PubSub: p2p.PubSub, |
| 49 | Topic: topic, |
| 50 | Sub: sub, |
| 51 | Inbound: make(chan *Message), |
| 52 | Outbound: make(chan string), |
| 53 | } |
| 54 | |
| 55 | go ps.handleInbound(p2p.Ctx) |
| 56 | go ps.handleOutbound(p2p.Ctx) |
| 57 | |
| 58 | return ps, nil |
| 59 | } |
| 60 | |
| 61 | func (ps *PubSub) handleInbound(ctx context.Context) { |
| 62 | for { |
no test coverage detected