Source-aware streaming demo

Receive source-published updates without repeatedly polling the REST API.

Source-aware streaming

WebSocket delivery pushes a new payload after OilPriceAPI receives an upstream update. Source publication cadence and network delivery time are different measurements, so inspect each value's source timestamp before using it.

Checking your account access…

What this channel currently carries

  • • Brent crude, WTI crude, and US natural gas payloads when available
  • • Source timestamps alongside delivered values
  • • ActionCable welcome, confirmation, ping, update, and rejection states
  • • Push delivery that reduces unnecessary polling

Quiet periods are expected. A connected channel can receive pings before the next upstream source publication.

Protocol-correct JavaScript

Native WebSocket using the ActionCable subscribe envelope implemented in production.

const token = 'YOUR_API_KEY';
const ws = new WebSocket(
  `wss://api.oilpriceapi.com/cable?token=${encodeURIComponent(token)}`,
  ['actioncable-v1-json']
);

ws.addEventListener('message', (event) => {
  const frame = JSON.parse(event.data);

  if (frame.type === 'welcome') {
    ws.send(JSON.stringify({
      command: 'subscribe',
      identifier: JSON.stringify({ channel: 'EnergyPricesChannel' })
    }));
    return;
  }

  if (frame.type === 'ping') return;
  if (frame.message) console.log('Price update:', frame.message);
});