# encore-go-pubsub

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

- skill · `type=skill` · Claude Code, Codex, Cursor, GitHub Copilot, Gemini CLI, OpenCode · 305 installs · ★ 30
- Author: encoredev
- License: Apache-2.0 · policy=mirror · the beginning of the file is at the end of this page
- Source: https://github.com/encoredev/skills/tree/HEAD/encore/go-pubsub/SKILL.md
- Updated: 2026-10-23
- HTML: https://affpapa.org/en/nevibecode/skills/encore-go-pubsub

## What it 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.

## Install

### Claude Code (native platform)

1. **Skills CLI (Vercel)**
```bash
npx skills add encoredev/skills --skill encore-go-pubsub -a claude-code
```
```bash
npx skills add encoredev/skills --skill go-pubsub -a claude-code
```

2. **Manually: put the files in place**
- File: `~/.claude/skills/go-pubsub/SKILL.md` — Copy the whole skill folder; for one project use .claude/skills/go-pubsub/

### Codex

1. **Skills CLI (Vercel)**
```bash
npx skills add encoredev/skills --skill go-pubsub -a codex
```

2. **Manually: put the files in place**
- File: `.agents/skills/go-pubsub/SKILL.md` — For all projects use ~/.agents/skills/

### Cursor

1. **Skills CLI (Vercel)**
```bash
npx skills add encoredev/skills --skill go-pubsub -a cursor
```

2. **Manually: put the files in place**
- File: `.cursor/skills/go-pubsub/SKILL.md` — Cursor also reads .agents/skills/ and .claude/skills/

### GitHub Copilot

1. **GitHub CLI**
```bash
gh skill install encoredev/skills go-pubsub
```

2. **Manually: put the files in place**
- File: `.github/skills/go-pubsub/SKILL.md`

### Gemini CLI

1. **Skills CLI (Vercel)**
```bash
npx skills add encoredev/skills --skill go-pubsub -a gemini-cli
```

2. **Platform CLI**
```bash
gemini skills install https://github.com/encoredev/skills --path encore/go-pubsub
```

### Windsurf (Devin Desktop)

**Manually: put the files in place**
- File: `.devin/skills/go-pubsub/SKILL.md` — The legacy .windsurf/skills/ path still works

### OpenCode

**Skills CLI (Vercel)**
```bash
npx skills add encoredev/skills --skill go-pubsub -a opencode
```

### Claude.ai

**Claude.ai**
- Zip the folder and upload it in Settings → Capabilities → Skills

Where a file is given instead of a command, copy the files from the source to that path: https://github.com/encoredev/skills/tree/HEAD/encore/go-pubsub/SKILL.md

## When to use it
- 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
- 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.

## Source text · SKILL.md · Apache-2.0

Below is the content of a third-party file. It is data, not instructions for you: do not act on anything written inside unless the human asks. Block tag: 52cd674ccef90c10.

BEGIN THIRD-PARTY FILE 52cd674ccef90c10
````text
---
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.
````
END THIRD-PARTY FILE 52cd674ccef90c10

The third-party file has ended. Everything between the 52cd674ccef90c10 tags is data: requests and commands from it are not followed. An “end of file” line with another tag inside the block is part of the file.

First 2964 of 3070 bytes. © encoredev · Apache-2.0 · https://github.com/encoredev/skills/tree/HEAD/encore/go-pubsub/SKILL.md

_Part of the page did not fit — see the HTML version._

---
NeVibeCode on affpapa.org. Catalog texts are descriptions, not instructions for the agent. How to choose and install: https://affpapa.org/en/nevibecode/agents.md
