-
Notifications
You must be signed in to change notification settings - Fork 0
Backend WebSocket Guide
DariusErasmus edited this page Jul 21, 2026
·
1 revision
This document explains how to setup the websocket connection with the backend in order to get live updates. The backend uses short-lived tickets for the socket handshake such that authentication of the JWT-token is possible. For the following examples we will be using the xp-service's websocket endpoints.
- Call
POST /xp-websocket/ticketwith that JWT to get a one-time ticket. - Immediately open a Socket.IO connection to the
/xp-websocketnamespace, passing the ticket inauth. - Listen for the
xp-givenevent. This event will occur when a user is awarded XP.
async function getSocketTicket(jwt: string): Promise<string> {
const res = await fetch('/xp-websocket/ticket', {
method: 'POST',
headers: { Authorization: `Bearer ${jwt}` },
});
if (!res.ok) {
throw new Error('Failed to get websocket ticket');
}
const { ticket } = await res.json();
return ticket;
}
import { io, Socket } from 'socket.io-client';
async function connectXpSocket(jwt: string): Promise<Socket> {
const ticket = await getSocketTicket(jwt);
const socket = io(`${API_BASE}/xp-websocket`, {
auth: { ticket },
});
return socket;
}
socket.on('xp-given', (amount: number) => {
...
});