Skip to content

Support custom HTTP clients#424

Draft
simolus3 wants to merge 6 commits into
worker-channel-detect-endfrom
custom-http-clients
Draft

Support custom HTTP clients#424
simolus3 wants to merge 6 commits into
worker-channel-detect-endfrom
custom-http-clients

Conversation

@simolus3
Copy link
Copy Markdown
Contributor

@simolus3 simolus3 commented Jun 2, 2026

Blocked by #425.

There are a number of cases where users might want to use custom HTTP clients with the PowerSync SDK, such as:

This PR adds support for this through an httpClient field on SyncOptions. It can be set to a Client Function() acting as a factory for HTTP clients. On native platforms, we send that factory function through a send port to reconstruct a client on the other end.

On the web, the whole thing is a little tricky. We can't send Dart objects to a worker, so this serializes HTTP requests and responses via postMessage calls:

  1. When the sync worker wants to issue an HTTP request, it sends the method, url, headers and body to a client.
  2. The client can then use the custom HTTP client to run the request, and replies with the status code and headers.
  3. The sync worker then requests chunks of the response, essentially mirroring the AsyncIterable interface through message ports.

We only do this when custom clients are installed, since postMessage might add overhead (probably not too much since we can transfer blobs though).

Note: According to mdn, ReadableStreams are supposed to be transferrable, which would have made the whole implementation much simpler. But also according to mdn, ReadableStreams aren't transferrable on Safari :/

TODOs:

  • Add unit test for native platforms.
  • Manually test this on the web.
  • On the web, recover from the client tab being closed while it's serving an HTTP request.

@simolus3 simolus3 changed the title Support custom HTTP requests Support custom HTTP clients Jun 2, 2026
@simolus3 simolus3 force-pushed the custom-http-clients branch from f6d7872 to 9bfb3ad Compare June 3, 2026 11:55
@simolus3 simolus3 changed the base branch from main to worker-channel-detect-end June 3, 2026 11:55
@simolus3 simolus3 force-pushed the custom-http-clients branch from e69c7e6 to 9533e1a Compare June 3, 2026 12:56
@T0b1i
Copy link
Copy Markdown

T0b1i commented Jun 3, 2026

I tested this PR against our self-hosted setup and it solves our CA-trust problem. Returning a package:http IOClient built on an HttpClient with a custom SecurityContext carrying our CA, via the Client Function() factory, connects fine and is fast. The factory covers the self-signed-cert case for us. I also tried a cronet_http client but it was extremely slow in the spawned sync isolate, and I haven't figured out why. We're going with dart:io, which works well.

Is there a concrete plan for when this feature will be officially released?

Thanks for the great work on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants