APITube Help Center

How to Stream News over WebSocket

Receive new articles and handle WebSocket messages

Erick Horn

Written by Erick Horn

August 22, 2026

Updated October 16, 2026

How to stream news over WebSocket

Connect to wss://api.apitube.io/v1/news/ws to receive new articles as JSON messages. Add your search filters to the URL. You can try the connection with wscat:

# npm install -g wscat
wscat -c "wss://api.apitube.io/v1/news/ws?api_key=YOUR_API_KEY&language.code=en"

Send your key through X-API-Key if your client supports headers, or through api_key. A missing or invalid key closes the socket with code 1008 (Unauthorized).

How does the WebSocket stream work?

The stream checks for matches about every 10 seconds and sends up to 50 articles per cycle. A heartbeat arrives about every 30 seconds.

Connections last up to six hours. At that point, ER0362 and close code 4005 ask you to reconnect. A fresh connection sends new matches, without a historical backlog. Search /v1/news/everything for older articles. Your plan’s data delay still applies.

What messages does the WebSocket send?

Read the type field to tell messages apart:

  • article: the article object is in data, such as msg.data.id or msg.data.title.
  • connected: confirms connection and includes a session_id.
  • subscribed: confirms the active filters.
  • heartbeat: keeps the connection active and includes a timestamp.
  • pong: replies to your ping.
  • error: includes a code and message.

This browser example reads articles and reports errors:

const ws = new WebSocket('wss://api.apitube.io/v1/news/ws?api_key=YOUR_API_KEY&language.code=en');

ws.onmessage = (event) => {
  const msg = JSON.parse(event.data);
  if (msg.type === 'error') { console.error(msg.code, msg.message); return; }
  if (msg.type !== 'article') return;              // connected / subscribed / heartbeat / pong — ignore
  const article = msg.data;                        // the article object lives under data
  console.log('new article', article.id, article.title);
};

How do I filter the stream and keep it alive?

Add language, category, source, sentiment or entity filters to the URL. Use fl to choose fields:

wscat -c "wss://api.apitube.io/v1/news/ws?api_key=YOUR_API_KEY&language.code=en&category.id=medtop:15000000&source.domain=reuters.com&fl=id,title,source.domain"

See how to select response fields. To check a quiet connection, send:

{"action":"ping"}

The server replies with {"type":"pong","timestamp":...}. Other client messages are ignored. To change filters, reconnect with a new URL. See the official endpoints reference.

What does WebSocket streaming cost?

Each delivered article uses one WebSocket credit. Connections, heartbeats and pongs are free. WebSocket has a separate quota from search and SSE.

When this quota is empty, delivery can use pay-as-you-go balance if enabled and within your spending limit. With no available funds at connection time, the server sends ER0404 and closes with 4002. If funds run out later, it sends ER0402 and closes.

If your pay-as-you-go spending limit has been reached, the server sends ER0177 and closes with 4006. Change the limit in the dashboard before reconnecting.

Common Questions

How is WebSocket different from SSE?

WebSocket accepts ping messages as well as sending articles. SSE is one-way, and its Last-Event-Id header supports resume after a disconnect. WebSocket needs your own reconnection logic and has no equivalent resume header.

For HTTP delivery to your server without an open connection, use a webhook.

How many WebSocket connections can I open at once?

Free allows 1, Starter and Basic allow 2 each, Professional allows 10, and Corporate allows 50. Going over the limit returns ER0403 and close code 4003. Closing a socket frees its slot.

Do test keys work over WebSocket?

Yes. Test keys return preview content without using your quota. Check article and control messages and reconnection, then switch to a live key. Free live keys also return preview article text.

What happens when my key is revoked?

An active stream stops on its next key check, with ER0361 and close code 4004. Reconnect with a valid key. Separate connections share the account’s available quota.


Related Articles