WebSocket Overview
The WebSocket API pushes articles to you as soon as they match your subscription. You open one connection, send one subscribe message and then receive articles in the same v3 format that REST and webhooks use.
Your plan must include WebSockets. The number of parallel connections and the number of delivered messages per period depend on your plan. WebSocket deliveries do not use credits. See Rate limits & credits and pricing.
Enriched and raw streams
There are two streams. Both use the same connection rules, the same control frames and the same article object.
| Stream | URL | What you get | First article on subscribe |
|---|---|---|---|
| Enriched | wss://wss.finlight.me | The article after enrichment: sentiment, and on request companies and events, as far as your plan allows. | Yes, the newest matching article. |
| Raw | wss://wss.finlight.me/raw | The article skeleton before enrichment. It arrives earlier but has no sentiment, companies or events. | No. |
Choose the enriched stream when you filter or act on companies, events or sentiment. Choose the raw stream when speed matters more than enrichment. See Raw stream for the details.
How it works
- Connect. Open a WebSocket to the URL and send your API key in the lowercase
x-api-keyheader. Browsers use?apiKey=instead. See Connect. - Subscribe. Send one JSON message with
"payloadVersion": 3and your filters. See Subscribe. - Receive. The server answers with
connection.admitted. The enriched stream then sends the newest matching article. After that, every new match arrives asarticle.created, and changes arrive asarticle.updatedwhen you ask for them. See Frames. - Stay connected. Send a ping every 25 seconds. Open a new connection before the 2 hour limit. See Lifecycle & limits.
The version is not part of the URL. You choose it in each subscribe message, and only the JSON number 3 selects v3. A subscribe without it, or with "3" as a string, gets the deprecated v2 format. See Versioning.
Stream with a client library
The client libraries handle the whole connection for you. You pass the subscribe fields and loop over the articles.
Each item has a kind:
created: a new article (article.created).updated: a changed article (article.updated). Only sent withincludeUpdates: true.other: an event name this library version does not know yet. Skip it.
The example follows NVIDIA news with companies and events. The companies block needs a plan with company data, and the events block needs Pro Standard or higher. Without the entitlement the key is just absent. See Plans & entitlements.
The PHP library has no WebSocket transport. It builds the subscribe message and decodes frames for your own socket loop. See its README.
Enriched stream
import { FinlightClient } from 'finlight-client'
const client = new FinlightClient({ apiKey: process.env.FINLIGHT_API_KEY! })
const stream = client.stream(
{ query: 'ticker:NVDA', language: 'en', includeUpdates: true, include: ['companies', 'events'] },
{
onReconnect: (r) => console.log('reconnected', r.reason, 'last seen', r.lastSeenAt),
onError: (e) => console.warn(e.message), // problems that do not end the stream
},
)
for await (const item of stream) {
if (item.kind === 'created' || item.kind === 'updated') {
console.log(item.kind, item.data?.title)
}
}
Connect without a client library
You can use any WebSocket library. This Node.js example uses the ws package. It connects, subscribes, pings every 25 seconds and prints what arrives. It does not reconnect; see Lifecycle & limits for what a long-running client must add.
Node.js (ws)
import WebSocket from 'ws'
const ws = new WebSocket('wss://wss.finlight.me', {
headers: { 'x-api-key': process.env.FINLIGHT_API_KEY }, // the header name must be lowercase
})
let pingTimer
ws.on('open', () => {
ws.send(
JSON.stringify({
payloadVersion: 3,
query: 'ticker:AMD',
language: 'en',
include: ['companies', 'events'],
includeUpdates: true,
clientNonce: 'my-client-1',
}),
)
pingTimer = setInterval(() => ws.send(JSON.stringify({ action: 'ping', t: Date.now() })), 25_000)
})
ws.on('message', (raw) => {
const frame = JSON.parse(raw.toString())
switch (frame.event ?? frame.action) {
case 'article.created':
case 'article.updated':
console.log(frame.event, frame.data.id, frame.data.title)
break
case 'connection.error':
console.error(frame.data.code, frame.data.message, frame.data.details ?? '')
break
default:
// connection.admitted, connection.pong, connection.preempted, or a v2 control frame
console.log(frame.event ?? frame.action)
}
})
ws.on('unexpected-response', (_req, res) => console.error('connect refused with HTTP', res.statusCode))
ws.on('close', (code) => {
clearInterval(pingTimer)
console.log('closed', code)
})
A typical start looks like this (article data shortened; see The article object for every field):
Frames after the subscribe
{"event":"connection.admitted","payloadVersion":3,"data":{"leaseId":"00000000-0000-4000-8000-00000000feed","serverNow":1795467845123,"clientNonce":"my-client-1"}}
{"event":"article.created","payloadVersion":3,"data":{"id":"pXhK9f2aQ1mZ0bT7cY3rLw","link":"https://www.reuters.com/markets/amd-zt-systems","source":{"domain":"www.reuters.com","type":"newswire"},"title":"AMD to buy ZT Systems","companies":[{"companyId":13,"name":"Advanced Micro Devices, Inc.","role":"acquirer","relevance":0.95}],"events":[{"type":"mna","direction":"positive","importance":"high","confidence":0.9,"companyIds":[13]}]}}
{"event":"connection.pong","payloadVersion":3,"data":{"t":1795467870123,"serverNow":1795467870160}}
What the client libraries do for you
Every v3 client library (TypeScript, Python, Go, .NET, Java, Ruby and Rust) does these things, so you do not have to:
- Sends the key in the lowercase
x-api-keyheader. - Sends
payloadVersion: 3, only the fields you set, and its ownclientNoncefor each connection. - Pings every 25 seconds and reconnects when no pong arrives for 60 seconds.
- Opens a new connection after about 115 minutes, before the 2 hour limit, and sends the same subscribe again.
- Drops duplicate frames, also across reconnects.
- Reads the v2 control frames the server sends before your first subscribe.
- Ends the stream with a typed error for problems that do not go away by retrying, such as a refused key or a rejected subscribe. It reconnects with a growing wait for the rest.
- Waits and retries once per hour when the WebSocket quota of the period is used up. You can turn this off.
- Reports the last
availableAtit saw (raw:firstSeenAt) after a reconnect, so you can fill gaps through REST. It does not backfill on its own.
See Client libraries for installation.
What's next
- Connect: URL, key, takeover and refused connections.
- Subscribe: every field of the subscribe message.
- Raw stream: what the raw stream accepts and sends.
- Frames: article frames and control frames.
- Lifecycle & limits: ping, rotation, preemption and quota.
- Browser client: a plain browser example.
- The article object: every field of
data.