Blog
Coinbase WebSocketsAccount events

Consuming Coinbase Market and User WebSocket Feeds

Separate Coinbase Advanced Trade market and user feeds, subscription health, JWT-backed setup, and account recovery without treating a socket as a complete account.

Siebly.io2 min readMarkdown

Attach the User Handler

A Coinbase market-data connection can keep delivering prices while the application's user-data connection is unavailable. Treating both as one connected flag conceals the loss of account events.

Advanced Trade has separate market and user endpoints. The coinbase-api stream client routes those connections through different keys. Track the scope actually required by each consumer.

The Coinbase tutorial explains supported credentials. The following module requires an appropriate Advanced Trade key and private signing material. It records message envelopes only; it does not construct an account cache:

coinbase-user-stream.mjs

JavaScript
import { WebsocketClient } from 'coinbase-api';

const apiKey = process.env.COINBASE_KEY_NAME;
const apiSecret = process.env.COINBASE_PRIVATE_KEY;
if (!apiKey || !apiSecret) throw new Error('Set the Coinbase signing credentials');

const client = new WebsocketClient({ apiKey, apiSecret });
client.on('update', (message) => {
  console.log({ channel: message.channel, type: message.type });
});
client.on('response', (response) => {
  console.log({ subscriptionResponse: response.type });
});
client.on('exception', () => {
  console.error('Coinbase stream exception');
});
client.on('reconnect', ({ wsKey }) => {
  console.error('Connection needs recovery:', wsKey);
});
const stop = () => client.closeAll();
process.once('SIGINT', stop);
setTimeout(stop, 20_000);
await client.subscribe(['user', 'heartbeats'], 'advTradeUserData');

The SDK supplies the supported authentication messages. Avoid logging complete private events while investigating a connection; they may contain account and order information.

Track Subscription Health

A heartbeat helps establish that the connection is communicating. It does not prove that every required topic was accepted or that a quiet user channel has an initial complete state.

Inspect subscription responses and protocol errors. When a subscription is rejected, do not wait indefinitely for its first data message. Track the user connection separately from public market-data freshness.

Initialize and Repair the Consumer

A handler must interpret the documented snapshot and update semantics for the subscribed channel. The first message is not automatically a complete account. Balance, position, order, and execution requirements can also require different reads.

After a known interruption, repair the facts needed by the affected consumer. The account-state article explains why an arbitrary REST response cannot simply overwrite every newer private event.

An order can complete while the user stream is unavailable. Look up the recorded identifiers and relevant history when resolving an unknown submission. Public prices continuing to arrive do not establish that this private recovery is complete.

Related articles

Continue from here

Related Siebly resources

All articles

Subscribe on Substack

Complete the Substack form below to join our newsletter. Substack handles all subscriber data directly.