Go - Topic.Publish()

Publish an event (push based message) to a topic.

import (
"context"
"github.com/nitrictech/go-sdk/nitric"
"github.com/nitrictech/go-sdk/nitric/topics"
)
func main() {
updates := nitric.NewTopic("updates").Allow(topics.TopicPublish)
updates.Publish(context.TODO(),
map[string]interface{}{
"something": "amazing happened",
},
)
nitric.Run()
}

Parameters

  • Name
    ctx
    Required
    Required
    Type
    context
    Description

    The context of the call, used for tracing.

  • Name
    event
    Required
    Required
    Type
    map[string]interface{}
    Description

    The event to publish to the topic.

  • Name
    opts
    Optional
    Optional
    Type
    PublishOption
    Description

    Optional function to send a message with a delay.

Examples

Publish a message

Publishing messages to a topic will push a copy of the message to each of the topic's subscribers. By default, delivery occurs without a delay.

import (
"context"
"github.com/nitrictech/go-sdk/nitric"
"github.com/nitrictech/go-sdk/nitric/topics"
)
func main() {
updates := nitric.NewTopic("updates").Allow(topics.TopicPublish)
updates.Publish(context.TODO(),
map[string]interface{}{
"something": "amazing happened",
},
)
nitric.Run()
}

Delaying message delivery

You can delay the delivery of messages sent to a topic. The current maximum delay is 7 days (604800 seconds).

import (
"context"
"time"
"github.com/nitrictech/go-sdk/nitric"
"github.com/nitrictech/go-sdk/nitric/topics"
)
func main() {
updates := nitric.NewTopic("updates").Allow(topics.TopicPublish)
updates.Publish(context.TODO(), map[string]interface{}{
"something": "amazing happened",
}, topics.WithDelay(time.Hour))
nitric.Run()
}
Last updated on Oct 15, 2024