Realtime events

The SDK includes a WebSocket client for live community updates: new messages, like counts, and moderation events.

How it works

  1. Call getWsTicket to obtain a short-lived WebSocket ticket for a community.
  2. Pass the ticket as the auth option to CommunityRealtimeClient.
  3. The client handles reconnection automatically.

React hook

import { useCommunityEvents } from '@coin-communities/sdk/react-query';
import { api } from '@coin-communities/sdk';
 
function CommunityFeed({ tokenAddress }) {
  useCommunityEvents(tokenAddress, {
    auth: async () => {
      const { data } = await api.getWsTicket({ path: { token_address: tokenAddress } });
      return data?.ticket ?? null;
    },
    onMessage: (event) => {
      // new message arrived — update your query cache or local state
    },
    onLike: (event) => {
      // like count updated
    },
  });
}

The hook automatically subscribes when the component mounts and unsubscribes on unmount. Handler callbacks are ref-wrapped so inline functions won't churn the connection.

Low-level client

import { CommunityRealtimeClient } from '@coin-communities/sdk/react';
 
const client = CommunityRealtimeClient.getOrCreate({
  baseUrl: 'https://api.coin-communities.xyz',
  tokenAddress: '7eYw...',
  auth: async () => {
    const { data } = await api.getWsTicket({ path: { token_address: '7eYw...' } });
    return data?.ticket ?? null;
  },
});
 
const dispose = client.subscribe({
  onMessage: (e) => console.log(e),
  onConnect:    () => console.log('connected'),
  onDisconnect: () => console.log('disconnected'),
});
 
// To stop receiving events from this handler:
dispose();