Add real-time WebSocket support; require Node 22 - #2
Merged
Conversation
Sync with python-client 0.3.0. Adds the three query-management endpoints (websocket/register, /fetch, /delete) and NewsDataApiWebSocket#stream, an async generator over the news matching a registered query. BREAKING: engines is now >=22 (was >=18). Streaming uses the global WebSocket, which Node ships from v22, keeping the package dependency free; the CI matrix moves to [22, 24]. Pass options.WebSocket to run on an older runtime. The server accepts every handshake and then closes with code 1008 on a permanent rejection (invalid credentials, api limit, device limit); those throw NewsdataWebSocketAuthError and are never retried. Every other close code, 1013 send-timeout included, reconnects with a capped exponential backoff. #request() now carries a per-endpoint HTTP method, and the websocket endpoints are exempt from the results-present success check.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
websocket/registerPOST(query params, no body)websocket/fetchGETwebsocket/deleteDELETERegister 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
409with the existingregistration_id.Streaming over
wss://ws.newsdata.io/ws/event?apikey=…®istration_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, ordevice limit reached(>5 devices on oneregistration_id). Those raise the new typed auth error and are never retried. Every other close code, including1013(send timeout— the client read too slowly), is transient and reconnects.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.enginesmoves from>=18to>=22, CI matrix to[22, 24]. Streaming uses the globalWebSocket, which Node ships from v22 — this keeps the package dependency-free, which is the reason for the bump. Users on older runtimes can passoptions.WebSocket(e.g. thewspackage).This warrants a version bump that signals the break.
Node specifics
stream(id)is an async generator; break out of the loop or callws.close()to stop.I originally added an HTTP probe to recover a 401/403 handshake status. The spec confirms the handshake is always accepted, so that was dead weight firing a spurious request on every failed connect — removed. There is now a test per documented 1008 reason, plus one proving
1013reconnects.47 tests pass under Node 22.