Go - Topic.Subscribe()

Subscribe a handler to a topic and receive new events for processing.

import (
  "fmt"

  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  nitric.NewTopic("updates").Subscribe(func() {
    fmt.Println("received update")
  })

  nitric.Run()
}

Parameters

  • Name
    handler
    Required
    Required
    Type
    interface{}
    Description

    The callback to be triggered when new messages are sent to the topic.

Examples

Subscribe to a topic

import (
  "fmt"

  "github.com/nitrictech/go-sdk/nitric"
  "github.com/nitrictech/go-sdk/nitric/topics"
)

func main() {
  nitric.NewTopic("updates").Subscribe(func(ctx *topics.Ctx) {
    fmt.Printf("received update %+v", ctx.Request.Message())
  })

  nitric.Run()
}

Notes

  • A function may only subscribe to a topic once, if multiple subscribers are required, create them in different functions.
  • A function may subscribe to OR publish to a topic but not both