WebSockets
Real-time, low-latency email event streaming
WebSockets provide a persistent, bidirectional connection to PostNuvia for receiving email events in real-time. Unlike webhooks, WebSockets don’t require a public URL or external tools like ngrok.
Why Use WebSockets?
Python SDK
The Python SDK provides both synchronous and asynchronous WebSocket clients.
Async Usage
Sync Usage
Event Handler Pattern
You can also use event handlers instead of iterating:
For sync usage with event handlers, run the listener in a background thread:
TypeScript SDK
The TypeScript SDK provides a WebSocket client with automatic reconnection.
Basic Usage
React/Next.js Usage
Using the SDK with React:
Subscribe Options
When subscribing to events, you can filter by inbox, pod, or event type:
Python:
TypeScript:
A subscription without event_types receives every event type except calendar events. It receives spam, blocked, and unauthenticated events only if the API key had the matching label visibility permission (label_spam_read, label_blocked_read, or label_unauthenticated_read) when the connection opened. Naming one of them in event_types without that permission makes the subscribe fail. Calendar events reach only subscriptions that name them in event_types. Each subscribe replaces the event types of any earlier subscription to the same inboxes or pods on that connection, so list every event type you want.
Event Types
Connection Events
Message Events
Domain Events
Calendar Events
Calendar events require the calendar_event_read permission, and reach only subscriptions that name them in event_types. See Calendar Webhooks for payloads and timing.
Message Properties
The event.message object contains:
Error Handling
Python:
TypeScript:
Copy for Cursor / Claude
Copy one of the blocks below into Cursor or Claude for WebSockets in one shot.
