Skip to content

Add real-time WebSocket support and useNewsStream - #2

Merged
arjunjain merged 1 commit into
mainfrom
feature/websocket
Aug 26, 2026
Merged

Add real-time WebSocket support and useNewsStream#2
arjunjain merged 1 commit into
mainfrom
feature/websocket

Conversation

@arjunjain

@arjunjain arjunjain commented Aug 26, 2026

Copy link
Copy Markdown
Collaborator

Syncs this client with python-client 0.3.0 (PR #27), which added the real-time WebSocket service.

What's here

Query management — three REST endpoints, verified against the OpenAPI 3.1 spec:

Endpoint Method
websocket/register POST (query params, no body)
websocket/fetch GET
websocket/delete DELETE

Register once, stream many times. A registered query has no date or paging filters — it matches news as it is published. Registering identical filters twice answers 409 with the existing registration_id.

Streaming over wss://ws.newsdata.io/ws/event?apikey=…&registration_id=…, with a capped exponential backoff (1s → 30s) and malformed frames skipped.

Failure model

Per the spec, the handshake is always accepted; a refused connection is then closed with code 1008 carrying one of three reasons — invalid credentials or registration not found, api limit reached, or device limit reached (>5 devices on one registration_id). Those raise the new typed auth error and are never retried. Every other close code, including 1013 (send timeout — the client read too slowly), is transient and reconnects.

⚠️ Each delivered article consumes 1 API credit per connected device.

Shared plumbing

The HTTP layer gained a per-endpoint method (it was GET-only), and the websocket endpoints are exempt from the results-present success check since their success envelope may omit results. Both touch the request path used by every endpoint — worth a look.

React specifics

No new dependency, and engines stays >=18 — browsers supply WebSocket.

Adds a useNewsStream(registrationId, options) hook that opens on mount, closes on unmount, and accumulates articles newest-first under a maxArticles cap (default 100) so a long-lived stream cannot grow without bound. A falsy id defers connecting, which is useful while registration is still resolving.

NewsDataApiWebSocket is also exported for imperative use outside React.

The src/core/ files stay byte-identical to nodejs-client’s, as before — only the WebSocket wording differs (browser vs Node).

53 tests pass.

Sync with python-client 0.3.0. Adds the three query-management endpoints
(websocket/register, /fetch, /delete) to the core client, the
NewsDataApiWebSocket class for imperative use, and a useNewsStream hook
that opens on mount, closes on unmount, and accumulates articles
newest-first under a maxArticles cap.

Uses the browser's global WebSocket, so the package stays dependency
free and engines stays >=18.

The server accepts every handshake and then closes with code 1008 on a
permanent rejection (invalid credentials, api limit, device limit);
those surface as NewsdataWebSocketAuthError and are never retried.

#request() now carries a per-endpoint HTTP method, and the websocket
endpoints are exempt from the results-present success check.
@arjunjain
arjunjain merged commit c6ebca6 into main Aug 26, 2026
3 checks passed
@arjunjain
arjunjain deleted the feature/websocket branch August 26, 2026 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant