Skip to main content
skill CX Codex

encore-go-pubsub — Claude Code skill: events and setup

Guides asynchronous messaging in Encore Go with topics and subscriptions. Use it for background handlers, event fan-out, and service-to-service messages.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add encoredev/skills --skill encore-go-pubsub -a claude-code
npx skills add encoredev/skills --skill go-pubsub -a claude-code
Open on GitHub

What encore-go-pubsub does

This skill shows how to define pubsub.NewTopic and pubsub.NewSubscription in Encore Go. A producer sends an event to a topic, while one or more consumers handle it through subscriptions. Both resources belong at package scope rather than inside functions.

It covers Publish, handlers that receive context.Context and return error, and restricted publishing through pubsub.TopicRef. Delivery can use pubsub.AtLeastOnce or pubsub.ExactlyOnce. With at-least-once delivery, duplicate messages are possible, so handlers must be idempotent. Publish returns after the message is queued; heavy synchronous work should stay out of the caller.

What it helps with

  • publish events between Go services
  • fan one event out to several handlers
  • run background reactions to messages
  • configure message delivery guarantees
  • pass publish access into library code

Example prompts for the agent

  • Create an Encore Go topic named OrderCreated with pubsub.AtLeastOnce delivery and a subscription that sends a confirmation email.
  • Write a handler that accepts context.Context and *OrderCreatedEvent, returning error when processing fails.
  • Show how to pass publishing permission through pubsub.TopicRef to a library function.

How to install encore-go-pubsub

1Skills CLI (Vercel)

npx skills add encoredev/skills --skill encore-go-pubsub -a claude-code
npx skills add encoredev/skills --skill go-pubsub -a claude-code

2Manually: put the files in place

~/.claude/skills/go-pubsub/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/go-pubsub/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about encore-go-pubsub

How do I install encore-go-pubsub in Claude Code?

Run npx skills add encoredev/skills --skill encore-go-pubsub -a claude-code, then use ~/.claude/skills/go-pubsub/SKILL.md.

What does encore-go-pubsub do?

It guides asynchronous event handling in Encore Go. Topics publish messages, while subscriptions attach background handlers and let one event reach multiple consumers.

What should handlers do with pubsub.AtLeastOnce delivery?

They must tolerate duplicate messages by being idempotent. A handler receives context.Context and the event, and returning an error lets the topic retry according to its retry policy.

Source text

SKILL.md · Apache-2.0
---
name: encore-go-pubsub
description: Asynchronous messaging in Encore Go via `pubsub.NewTopic` and `pubsub.NewSubscription` from `encore.dev/pubsub` — broadcast events, decouple producers from consumers, and run background handlers.
when_to_use: >-
  User wants to publish/broadcast events, fan out a single event to many handlers, fire-and-forget messages between Go services, react to something asynchronously, set up a worker that consumes events, configure delivery guarantees (at-least-once, exactly-once), or use ordering attributes. Trigger phrases: "publish an event", "broadcast", "subscribe to", "topic", "Pub/Sub", "pubsub", "event bus", "OrderCreated event", "send to anyone listening", "background event handler", "queue", "fan out".
---

# Encore Go Pub/Sub

## Instructions

Pub/Sub is for asynchronous messaging between services. Producers publish events to a `Topic`; consumers attach `Subscription`s to react. Resources must be declared as package-level variables — never inside functions.

## Topics

```go
package events

import "encore.dev/pubsub"

type OrderCreatedEvent struct {
    OrderID string `json:"order_id"`
    UserID  string `json:"user_id"`
    Total   int    `json:"total"`
}

// Package level declaration
var OrderCreated = pubsub.NewTopic[*OrderCreatedEvent]("order-created", pubsub.TopicConfig{
    DeliveryGuarantee: pubsub.AtLeastOnce,
})
```

### Publishing

```go
msgID, err := events.OrderCreated.Publish(ctx, &events.OrderCreatedEvent{
    OrderID: "123",
    UserID:  "user-456",
    Total:   9999,
})
```

### Subscriptions

```go
package notifications

import (
    "context"
    "myapp/events"
    "encore.dev/pubsub"
)

var _ = pubsub.NewSubscription(events.OrderCreated, "send-confirmation-email",
    pubsub.SubscriptionConfig[*events.OrderCreatedEvent]{
        Handler: sendConfirmationEmail,
    },
)

func sendConfirmationEmail(ctx context.Context, event *events.OrderCreatedEvent) error {
    // Send email...
    return nil
}
```

### Topic References

Pass topic access to library code while maintaining static analysis:

```go
// Create a reference with publish permission
ref := pubsub.TopicRef[pubsub.Publisher[*OrderCreatedEvent]](OrderCreated)

// Use the reference in library code
func publishEvent(ref pubsub.Publisher[*OrderCreatedEvent], event *OrderCreatedEvent) error {
    _, err := ref.Publish(ctx, event)
    return err
}
```

## Delivery Guarantees

- `pubsub.AtLeastOnce` (default): may deliver duplicates → handlers must be idempotent.
- `pubsub.ExactlyOnce`: stricter, capped throughput (AWS 300 msg/s/topic, GCP 3000+ msg/s/region). Does not deduplicate on the publish side.

## Guidelines

- Topics and subscriptions must be declared as package-level variables.
- Subscription handlers must be idempotent (at-least-once delivery is the default).
- Subscription handlers receive a `context.Context` and the event pointer; return an `error` to retry per the topic's retry policy.
- Don't do heavy synchronous work in `Publish` callers — `Publish` returns once the message is queued.

© encoredev · Apache-2.0 · encoredev/skills

Similar skills

SKILL.md skills catalog →

From the same set25

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor