Skip to main content

WebSocket quick start

Real-time updates from Launchpad in a few lines. The SDK owns one socket.io connection (StreamWebSocketService); the global feed is public, so it needs no authentication.

Installation​

npm install @gala-chain/launchpad-sdk@next @gala-chain/gala-core-sdk@next

1. Subscribe to the global feed​

import { StreamWebSocketService } from '@gala-chain/launchpad-sdk';

const service = new StreamWebSocketService({ url: 'wss://stream.example.com' });

// Opens the socket, joins the feed once connected, and returns this subscription's own unsubscribe.
const unsubscribe = service.subscribeToGlobalFeed({
onTradeExecuted: (event) => {
console.log(`Trade on ${event.tokenName}: ${event.tradeType}`);
},
onStreamGlobalStatus: (event) => {
console.log(`Streaming ${event.enabled ? 'enabled' : 'disabled'}`);
},
});

The same service is sdk.realtime on a LaunchpadSDK built with a streamWebSocketUrl.

2. One callback for every token event​

const stop = service.subscribeFeedTokenEvents((feedEvent) => {
// `feedEvent.event` names the event; `feedEvent.data` is its payload.
console.log(feedEvent.event, feedEvent.data.tokenName);
});

3. Filter on the client​

The server filters nothing: subscribe to the feed and filter in your callback.

const watched = new Set(['anime', 'dragnrkti']);

const unsubscribeFiltered = service.subscribeToGlobalFeed({
onTradeExecuted: (event) => {
if (watched.has(event.tokenName)) console.log(`Trade on watched token ${event.tokenName}`);
},
});

4. A token's room​

// Detailed events of one token: chat, reactions, viewers, status.
const leave = service.subscribeToken('STREAM_CHAT_MESSAGE', 'anime', (event) => {
console.log(`[${event.user?.fullName ?? event.userAddress}]: ${event.content}`);
});

5. Authenticate for user events and chat​

const authenticated = new StreamWebSocketService({
url: 'wss://stream.example.com',
// Asked at every authentication and every reconnect: answer with the token current at that moment.
tokenProvider: async () => process.env.APP_JWT ?? null,
});

const ok = await authenticated.authenticate();
console.log(ok ? 'authenticated' : 'not authenticated');

See Authentication for refresh and reconnect behavior.

6. React to reconnects and disconnects​

const stopReconnect = service.onReconnect(() => {
console.log('Reconnected: refetch what you rendered from the feed');
});

const stopState = service.onConnectionStateChange((connected) => {
console.log(connected ? 'online' : 'offline');
});

7. Clean up​

unsubscribe();
unsubscribeFiltered();
stop();
leave();
stopReconnect();
stopState();
service.disconnect();
authenticated.disconnect();

Troubleshooting​

  • No events: confirm service.isConnected(); the socket opens when the first subscription is registered (autoConnect, default true).
  • Too many events: filter in the callback, or subscribe to the one event you need (service.subscribeFeed('TRADE_EXECUTED', listener)).
  • Memory: every subscribe* call returns an unsubscribe; call it when the consumer goes away.

Next steps​