DEV Community

Cover image for Streaming Progress from Background AI Agents with AppSync Events
Sascha Mirow-Stanitzok
Sascha Mirow-Stanitzok

Posted on Originally published at sascha-ms.de

Streaming Progress from Background AI Agents with AppSync Events

A user clicks "Find me a book", an agent starts researching, calling tools and writing, and for a full minute nothing visibly happens. So they click again, and you pay for the run twice. Long-running agents need live feedback, and on AWS there are three serious ways to deliver it. I compared them and built the least-known one in AppSync Events vs. SSE vs. WebSocket for AI agents.

Three options, one deciding question

  • Server-Sent Events via Lambda response streaming: lean and needs no extra service. But the stream lives exactly as long as the Lambda invocation. If the connection drops, the browser loses the run while Lambda keeps computing.
  • API Gateway WebSocket API: bidirectional and fully under your control. You also own the connection table, the fan-out, cleaning up dead connections and the limits of two hours per connection and ten minutes idle time.
  • AppSync Events: managed pub/sub over WebSockets. Publishers post to a channel, subscribers listen, and AWS handles connections, fan-out and keep-alive. Despite the name, there is no GraphQL involved.

My decision guide boils down to this: if the result comes back in the same request (a chat, a summarize button), SSE is enough. If the client sends a lot or you need your own protocol, a WebSocket API pays off. If the work runs in the background and the browser mostly listens, AppSync Events is the better default. For small status events, the two WebSocket-based options end up in a similar price range; the full article has the breakdown.

The pattern that makes it work

The example is a reading advisor for a bookshop: accept the job, answer 202 right away, let a worker Lambda do the steps and publish each one to the channel /agentjobs/<jobId>.

The subtle part is the order. A subscriber only receives events that are published after it subscribed. So the browser creates the job ID itself, subscribes, waits for subscribe_success and only then starts the job:

browser: jobId = random UUID
browser: subscribe /agentjobs/<jobId>   -> wait for subscribe_success
browser: POST /jobs { jobId, question } -> 202 Accepted
worker:  publish step ... step, result, done   (IAM-signed HTTP)
AppSync: deliver every event to all subscribers of the channel
Enter fullscreen mode Exit fullscreen mode

No race, no missing first steps. The worker knows nothing about connections; whether two tabs, a phone or nobody is watching makes no difference to it. That decoupling is why AppSync Events has become my default for agent runs.

Security and the gotchas worth knowing

A few details separate a demo from something you can run in production:

  • Two auth modes. Browsers connect and subscribe with an API key, but only IAM-signed callers may publish. Copying the key from the page source does not let anyone fake progress messages.
  • Reject wildcard subscriptions. Without an onSubscribe handler, anyone with the (public) API key could subscribe to /agentjobs/* and read every job. A tiny APPSYNC_JS handler allows only channels whose second segment is a UUID.
  • Events are strings. The events field expects JSON strings, at most five per publish request and at most 240 KB per event. Larger events are not delivered and nobody notices, so send an ID and fetch big results over HTTP.
  • Turn off async retries on the worker (maximumRetryAttempts: 0), otherwise a failure means the browser sees every step twice.
  • API keys expire after seven days unless you set an expiry, so plan rotation or move to Cognito.

I think AppSync Events is undervalued because many people hear "AppSync", think of GraphQL and move on. I am also skeptical of streaming long agent runs over SSE from a single invocation, because it ties the work to one browser tab. The trade-offs are real, though: events are not kept for late subscribers, so a page reload needs a stored status, and the protocol is AWS-specific.

What the full article covers

  • A side-by-side comparison of direction, lifetime, limits and pricing
  • The complete Serverless Framework setup for the Event API, namespace and API key
  • A SigV4 publisher and a library-free browser WebSocket client in TypeScript
  • Testing with wscat plus offline unit tests without an AWS account
  • A cost estimate, security rules and an FAQ

All details and the full code are here: AppSync Events vs. SSE vs. WebSocket for AI agents

๐Ÿ“– This is the condensed version. The full article on sascha-ms.de goes deeper โ€“ with all code samples, diagrams and a short knowledge check.


๐Ÿ‘‰ Next step: Curious what your own Lambda functions cost? Try my Lambda cost calculator โ€“ it runs right in your browser.

๐Ÿ“ฌ Stay updated: New articles on serverless, AI and leading tech teams via RSS, browser push or the weekly newsletter (in German).

Originally published on sascha-ms.de. More articles on serverless, AI in everyday development and leading tech teams: sascha-ms.de/en

Top comments (0)