Using a google cloud platform to implement pubsub model and using functions to create a topic, subscriber, publish and pullmsg function.
func pullMsgs(projectID, subID string, jsonPath string) error {
ctx := context.Background()
client, err := pubsub.NewClient(ctx, projectID, option.WithCredentialsFile(jsonPath))
if err != nil {
return fmt.Errorf("pubsub.NewClient: %v", err)
}
// Consume 10 messages.
var mu sync.Mutex
received := 0
sub := client.Subscription(subID)
cctx, cancel := context.WithCancel(ctx)
err = sub.Receive(cctx, func(ctx context.Context, msg *pubsub.Message) {
mu.Lock()
defer mu.Unlock()
// fmt.Fprintf(w, "Got message: %q\n", string(msg.Data))
fmt.Println("Got message: n", string(msg.Data))
msg.Ack()
received++
if received == 10 {
cancel()
}
})
if err != nil {
return fmt.Errorf("Receive: %v", err)
}
return nil
}
pullmsg function uses subscription Id to get published message from the publisher. Assume that model has 3 subscriber for the particular topic. if the publisher publishes msg for that topic. pullmsg function has to be executed 3 times to get that message for all subscribers. Is there any method to get published message to all subscribers at a single shot.