Skip to content

SDK Examples

Use any Centrifugo client SDK. The token returned by POST /v2/subscribe is a Centrifugo connection JWT — pass the returned url and token directly to the SDK constructor.

import { Centrifuge } from 'centrifuge'
 
const API_KEY = process.env.DLK_API_KEY!
const MATCH_ID = 28850808
 
// 1. Subscribe and get a Centrifugo connection token
const res = await fetch(`https://live.deadlock-api.com/v2/subscribe`, {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${API_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ match_id: MATCH_ID }),
})
const { url, channel: channelName, token } = await res.json()
 
// 2. Connect — the JWT carries a pre-authorised `subs` claim, so no second
//    round-trip is needed for the channel.
const client = new Centrifuge(url, { token })
const sub = client.newSubscription(channelName)
 
// 3. Listen for events
sub.on('publication', (e) => {
  if (e.data?.event_type === 'end') {
    console.log('Match ended')
    client.disconnect()
    return
  }
  console.log(e.data)
})
 
sub.subscribe()
client.connect()

Token expires in expires_in seconds. Refresh via POST /v2/token-refresh and call client.setToken(newToken) to swap tokens on the live connection without disconnecting.