Connect to websocket
Learn how to implement real-time features using Whop’s websocket API
You can connect to the websocket from your client side frontend code running in the iFrame.
-
Create the websocket client:
-
Add event handlers for messages:
-
Handle connection status changes:
-
Connect to the websocket and start receiving events:
-
Optional: Disconnect from the websocket:
Send messages from the client
You can send messages from the client to the server by using the websocket.broadcast
function.
-
Create a websocket client as above, and make sure you are connected by calling
websocket.connect()
. -
Send a custom message via websocket.
The target field is the same as the one you would pass to
whopApi.sendWebsocketMessage
on the server.
Send messages from your server
You can broadcast trusted websocket messages from your server to connected clients by using the whopApi.sendWebsocketMessage
function.
-
Construct an instance of the whop server sdk and pass your API key:
-
Send a custom string message via websocket.
Receive messages on your server
Before you start, make sure you are using NodeJS 22.4 or higher, or Bun to run your server.
Use the server websocket API to receive events such as chat messages as forum posts for a particular user on your server. You can use these events to build real-time apps such as chat bots and AI-agents that react to events on the platform.
-
Construct (or reuse) an instance of the whop server sdk and pass your API key:
-
Create your websocket client and add handlers for messages / status changes:
-
Add event handlers for messages:
-
Add event handlers for status changes (same as client API):
-
Connect to the websocket and start receiving events:
-
Optional: Disconnect from the websocket:
React Example
You can use the WhopWebsocketProvider
component to wrap your app and provide the websocket client and status to your components.