Skip to content

Add real-time WebSocket support - #13

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

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

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), is transient.

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

PHP specifics — the dependency is optional

PHP has no WebSocket client in core, so stream() needs phrity/websocket. It is declared in suggest only, not require, because:

  • it needs PHP 8.1, while this package supports 7.3+;
  • CI runs 7.4 → 8.4, so a hard require (even in require-dev) would fail composer install on the 7.4 and 8.0 jobs;
  • that would break REST-only users to add a feature they may never call.

Everything else — including the three management endpoints — works without it on every supported PHP. stream() throws a NewsdataWebSocketError naming the package if it is missing, and the classes are referenced dynamically so the file loads and passes PHPStan without it installed.

Tell me if you would rather bump the floor to 8.1 and make it a hard require — that is a one-line change.

Shared plumbing

execute() gained a per-endpoint HTTP method (it was GET-only via cURL), and the websocket endpoints are exempt from the results-present success check.

31 tests pass; PHPStan and php-cs-fixer clean.

Sync with python-client 0.3.0. Adds the three query-management endpoints
(get_websocket_register / _fetch / _delete) and NewsdataWebSocket::stream,
a generator over the news matching a registered query.

phrity/websocket is an OPTIONAL dependency (suggest only), not a hard
require: it needs PHP 8.1 while this package supports 7.3+, and CI runs
7.4 through 8.4 — requiring it would break composer install on the older
jobs for REST users who may never stream. Everything except stream()
works without it; stream() throws a NewsdataWebSocketError naming the
package when it is missing. The class is referenced dynamically so the
file loads and analyses without it installed.

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.

execute() now carries a per-endpoint HTTP method, and the websocket
endpoints are exempt from the results-present success check.
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 64.58333% with 51 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/NewsdataWebSocket.php 74.33% 29 Missing ⚠️
src/NewsdataApiBase.php 0.00% 16 Missing ⚠️
src/NewsdataApi.php 60.00% 6 Missing ⚠️

📢 Thoughts on this report? Let us know!

@arjunjain
arjunjain merged commit b8e4fee into main Aug 26, 2026
9 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.

2 participants