WebSockets

Pro Feature

This feature is only available on paid plans. Please schedule a demo or email us to get started.

Fern’s SDKs support sending and receiving messages over WebSockets. You can specify your WebSocket API Definition by using AsyncAPI or the Fern Definition.

How it works

1

Specify your WebSocket API Definition

2

Generate the SDK

1import { ChatClient } from "chat";
2
3const client = new ChatClient();
4
5const socket = client.chat.connect({
6 channelId: "123",
7 onOpen: () => {
8 console.log("Connected to the WebSocket");
9 },
10 onMessage: (message) => {
11 console.log(message);
12 },
13});
14
15await socket.send("Hello, world!");
16
17socket.on("close", () => {
18 console.log("WebSocket connection closed");
19});
20
21await socket.send("Bye, world!");
22
23socket.close();