Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions fern/pages/websockets.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ The TypeScript SDK provides a WebSocket client with automatic reconnection.
### Basic Usage

```typescript
import { AgentMailClient, AgentMail } from "agentmail";
import { AgentMailClient } from "agentmail";

const client = new AgentMailClient({
apiKey: process.env.AGENTMAIL_API_KEY,
Expand All @@ -143,11 +143,11 @@ async function main() {
});
});

socket.on("message", (event: AgentMail.Subscribed | AgentMail.MessageReceivedEvent) => {
socket.on("message", (event) => {
if (event.type === "subscribed") {
console.log("Subscribed to:", event.inboxIds);
} else if (event.type === "message_received") {
console.log("New email from:", event.message.from_);
} else if (event.type === "event" && event.eventType === "message.received") {
console.log("New email from:", event.message.from);
console.log("Subject:", event.message.subject);
}
});
Expand Down Expand Up @@ -193,7 +193,7 @@ function useAgentMailWebSocket(apiKey: string, inboxIds: string[]) {
});

socket.on("message", (event) => {
if (event.type === "message_received") {
if (event.type === "event" && event.eventType === "message.received") {
setLastMessage(event);
}
});
Expand Down Expand Up @@ -320,7 +320,7 @@ The `event.message` object contains:
| `inbox_id` | `inboxId` | Inbox that received the email |
| `message_id` | `messageId` | Unique message ID |
| `thread_id` | `threadId` | Conversation thread ID |
| `from_` | `from_` | Sender email address |
| `from_` | `from` | Sender email address |
| `to` | `to` | Recipients list |
| `subject` | `subject` | Subject line |
| `text` | `text` | Plain text body |
Expand Down Expand Up @@ -355,7 +355,7 @@ async def main():

**TypeScript:**
```typescript
import { AgentMailClient, AgentMail, AgentMailError } from "agentmail";
import { AgentMailClient, AgentMailError } from "agentmail";

const client = new AgentMailClient({
apiKey: process.env.AGENTMAIL_API_KEY,
Expand All @@ -372,8 +372,8 @@ async function main() {
});
});

socket.on("message", (event: AgentMail.MessageReceivedEvent) => {
if (event.type === "message_received") {
socket.on("message", (event) => {
if (event.type === "event" && event.eventType === "message.received") {
processEmail(event.message);
}
});
Expand Down Expand Up @@ -430,7 +430,7 @@ Copy one of the blocks below into Cursor or Claude for WebSockets in one shot.
*
* const socket = await client.websockets.connect();
* socket.on("open", () => socket.sendSubscribe({ type: "subscribe", inboxIds: [...] }));
* socket.on("message", (e) => { if (e.type === "subscribed") ...; if (e.type === "message_received") ... });
* socket.on("message", (e) => { if (e.type === "subscribed") ...; if (e.type === "event" && e.eventType === "message.received") ... });
* socket.on("close" | "error", ...);
*/
import { AgentMailClient } from "agentmail";
Expand All @@ -442,7 +442,7 @@ Copy one of the blocks below into Cursor or Claude for WebSockets in one shot.
socket.on("open", () => socket.sendSubscribe({ type: "subscribe", inboxIds: ["agent@agentmail.to"] }));
socket.on("message", (e) => {
if (e.type === "subscribed") console.log(e.inboxIds);
if (e.type === "message_received") console.log(e.message.subject);
if (e.type === "event" && e.eventType === "message.received") console.log(e.message.subject);
});
}
main().catch(console.error);
Expand Down