Skip to content

Add real-time WebSocket support - #2

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

Add real-time WebSocket support#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.

Java specifics

No new dependency — built on Java 11’s java.net.http.WebSocket.

stream(id, handler) blocks; the handler returns true to continue, false to stop. AutoCloseable, so try-with-resources ends an in-flight stream.

Tests run against a minimal RFC 6455 mock server (server→client frames are unmasked, so it stays short). 50 tests pass.

Sync with python-client 0.3.0. Adds the three query-management endpoints
(websocket/register, /fetch, /delete) and NewsDataApiWebSocket, which
streams the news matching a registered query via a handler predicate.

Built on Java 11's java.net.http.WebSocket, so no new dependency. The
server accepts every handshake and then closes with code 1008 on a
permanent rejection (invalid credentials, api limit, device limit);
those throw NewsdataWebSocketAuthException and are never retried.

request() now carries a per-endpoint HTTP method, and the websocket
endpoints are exempt from the results-present success check. Tests run
against a minimal RFC 6455 mock server.
@arjunjain
arjunjain merged commit f6c1154 into main Aug 26, 2026
2 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