HTTP & WebSockets
Network requests in PAM Desktop run on host threads, with the system’s trust store, and answer PHP through callbacks or promises. PHP never blocks on a socket, so a slow server cannot freeze the window. The host enforces HTTPS, size limits and timeouts for you. For streaming data, the WebSocket module keeps a connection open in the host and wakes PHP only when a message arrives.
Making requests
Section titled “Making requests”use Pam\Desktop\Http\Http;use Pam\Desktop\Http\HttpResponse;
Http::get('https://api.example.com/invoices', function (HttpResponse $response): void { if ($response->transportFailed()) { $this->error = $response->error; // offline, DNS, TLS, timeout return; } if ($response->successful()) { $this->invoices = json_decode($response->body, true); }});
Http::json('POST', 'https://api.example.com/invoices', ['total' => 4200], function (HttpResponse $response): void { $this->created = $response->statusCode === 201;});| Call | Notes |
|---|---|
Http::get($url, $callback) |
|
Http::post, put, patch, delete($url, $callback, $headers, $body, $timeoutMs) |
|
Http::request($method, $url, $callback, $headers, $body, $timeoutMs) |
|
Http::json($method, $url, $data, $callback, $headers, $timeoutMs) |
Encodes $data and sets the JSON headers. |
Http::upload(...), Http::uploadWithProgress(...) |
PUT a file of the sandbox without reading it into PHP. |
Http::multipart($url) |
Starts a multipart form upload. |
The callback always receives an HttpResponse. When no response arrived, statusCode is 0, error holds the message and transportFailed() is true. An HTTP error status is a normal response: check successful() or statusCode.
Rules the host enforces
Section titled “Rules the host enforces”- Requests go to
https://only. Plainhttp://is accepted for loopback hosts, and for any host underpam desktop dev. - Redirects are never followed: a 3xx response is returned as it is.
- Certificates are verified against the system trust store.
- A request body holds at most one mebibyte and a response body at most 900 KiB; a larger response fails. Use
Files::download()for large files. - Methods are GET, POST, PUT, PATCH and DELETE. The timeout is 30 seconds by default, from 1 to 120 seconds.
API clients
Section titled “API clients”Http::create() returns an HttpClient with a base URL, default headers, a timeout and retries. Its requests return a pending response with then() and catch(); a transport failure or a status of 400 and above rejects with an HttpError:
<?php
declare(strict_types=1);
namespace App\Services;
use Pam\Desktop\Http\Http;use Pam\Desktop\Http\HttpClient;use Pam\Desktop\Http\HttpError;
final class Api{ public static function client(string $token): HttpClient { return Http::create('https://api.example.com/v1', ['Authorization' => "Bearer {$token}"], timeout: 15_000, retry: 2) ->onError(function (HttpError $error): null { error_log('API error: '.$error->getMessage());
return null; // keep the rejection; return a Response to recover }); }}use Pam\Desktop\Http\HttpError;use Pam\Desktop\Http\Response;
$this->api->get('/invoices', ['page' => 2]) ->then(fn (Response $response) => $this->invoices = $response->json('data', [])) ->catch(fn (HttpError $error) => $this->error = $error->isNetworkError() ? 'You are offline.' : $error->getMessage());withHeaders(), withBaseUrl(), withTimeout(), withRetry($times, $delayMs), onRequest(), onResponse() and onError() return configured copies. A pending response can be cancelled with cancel(), for example when the user leaves the screen.
Uploads
Section titled “Uploads”Upload a file from the sandbox without loading it into PHP memory:
use Pam\Desktop\Http\TransferProgress;
$this->upload = Http::uploadWithProgress( 'https://uploads.example.com/videos/42', 'recordings/demo.mp4', // a path in the file sandbox fn (HttpResponse $response) => $this->uploaded = $response->successful(), fn (TransferProgress $progress) => $this->percent = $progress->percent(),);
// If the user cancels:Http::cancelTransfer($this->upload);HttpClient::upload($url, $path, $headers, $progress) and HttpClient::multipart() do the same with the promise API.
WebSockets
Section titled “WebSockets”Pam\Desktop\Network\WebSocket keeps a text WebSocket open in the host. A native reader waits for messages; PHP does not poll:
use Pam\Desktop\Modules\NativeModuleException;use Pam\Desktop\Network\WebSocket;use Pam\Desktop\Network\WebSocketEvent;
public function mount(): void{ $this->socket = WebSocket::connect( 'wss://example.com/live', function (WebSocketEvent $event, string $text): void { match ($event) { WebSocketEvent::Open => $this->online = true, WebSocketEvent::Text => $this->messages[] = $text, WebSocketEvent::Closed, WebSocketEvent::Failed => $this->online = false, }; }, fn (NativeModuleException $error) => $this->error = $error->getMessage(), owner: $this, );}
public function send(string $text): void{ $this->socket->send(json_encode(['type' => 'message', 'text' => $text]));}- With an
ownercomponent, the socket is disposed when the component unmounts. - Only
wss://is allowed, exceptws://to a loopback address or underpam desktop dev. - At most 4 connections at a time; messages are text, at most 64 KiB.
- At most 32 events or 256 KiB wait for PHP. A client that cannot keep up gets a
Failedevent instead of silently losing messages. - Reconnection is left to the application. Back off between attempts, and do not reconnect while the window is in the background.
For Laravel Reverb private channels, the bundled Pam\Desktop\Realtime package binds subscriptions to a component’s lifecycle; see Bundled Packages.
Being a good network citizen
Section titled “Being a good network citizen”- Fetch on
mount()or on a user action, not on a fast timer. When polling is unavoidable, use$this->timers()->every(), which pauses while the application is in the background. - Watch connectivity with
DeviceStatus::watch()and skip requests whilenetworkTypeisNetworkType::None; see System Services. - Cache what does not change: images and media have their own caches; store API responses in
Storageor SQLite.
Next steps
Section titled “Next steps”- Storage & Files for downloads into the sandbox.
- Security for the network rules in context.