Search by

neuron-core / iq-stream-php

valerione

IQ-Stream official client library for the PHP ecosystem.

Package info

github.com/neuron-core/iq-stream-php

pkg:composer/neuron-core/iq-stream-php

Statistics

Installs: 46

Dependents: 2

Suggesters: 2

Stars: 0

Open Issues: 0

1.1.0 2026-11-27 14:01 UTC

This package is auto-updated.

Last update: 2026-11-27 14:04:17 UTC


README

The PHP client of the IQ-Stream server, which relays an AI agent's answer from your backend to the browser over Server-Sent Events. Your PHP code sends the answer to the server as the model generates it, and the browser shows it as it arrives.

You need PHP 8.1 or later, an IQ-Stream server your backend can reach, and the key and secret of an app on that server.

Stream your first answer in three steps

1. Install the package

composer require neuron-core/iq-stream-php

It has no dependencies. To reach a server over https://, PHP needs the openssl extension, which most installations have.

2. Create the client

Put the server's address and your app's credentials in your environment, for example in .env:

IQ_STREAM_URL=https://<region>.iq-stream.dev
IQ_STREAM_APP_KEY=my-app
IQ_STREAM_APP_SECRET=a-secret-of-at-least-32-bytes

Then create the client from them:

use IQStream\IQClient;

$iq = IQClient::fromEnvironment();

Or pass the same values yourself:

use IQStream\Credentials;
use IQStream\IQClient;

$iq = new IQClient(
    'https://<region>.iq-stream.dev',
    new Credentials('my-app', $secret),
);

The client holds your app's secret, so it belongs to your backend. The browser never sees the secret: it gets a ticket, which lets it read one answer.

3. Publish the answer

Each answer is a stream of its own. Making a stream takes two parts of your code: the request that starts the chat, and the code that runs the agent.

Give the browser a ticket. In the request that starts the chat, make a ticket and send it to the page. Start the agent from there, for example in a queued job, and give it the stream's id:

$ticket = $iq->ticket();  // a new stream, and what the browser needs to read it

dispatch(new AnswerChat($messages, $ticket->streamId->value));  // The background process that will publish the stream

header('Content-Type: application/json');
echo json_encode($ticket);  // {"streamId": ..., "token": ..., "url": ..., "expiresAt": ...}

Send the answer as it is generated. Where the agent runs, open a publisher on that stream and send each chunk of the model's output:

use IQStream\StreamId;

$publisher = $iq->publisher(new StreamId($streamId));  // the connection to the server opens now

foreach ($agent->stream($messages) as $chunk) {
    $publisher->send(json_encode($chunk));  // reaches the browser at once
}

$publisher->close();   // tells the browser the answer is complete

AnswerChat stands for your own job, $agent->stream() for your framework's streaming output. Each send() is one event in the browser: send strings, usually JSON.

The browser can subscribe before the agent starts or while it runs: the server keeps the answer's events until the stream is closed, and gives a late browser everything it missed.

Check that it arrives. Open the ticket's URL with curl, then run the agent. Each chunk appears as it is sent, and the stream ends with iq:end:

curl -N "https://<region>.iq-stream.dev/<streamId>?token=<token>"

In the page, read the ticket with the browser client of @neuron-core/iq-stream:

import { subscribe } from '@neuron-core/iq-stream/client';

const ticket = await (await fetch('/chat', { method: 'POST', body })).json();

subscribe(ticket.url, {
  onFrame: (frame) => render(JSON.parse(frame.data)),
  onClose: (outcome) => {
    if (outcome.type === 'gone') loadAnswerFromBackend();  // the page missed part of it: load what you saved
  },
});

A plain new EventSource(ticket.url) works too.

That's it: your answer is streaming.

Good to know

  • Run the agent in a process that lives as long as the answer: a queued job, a worker, a console command. The publisher keeps one connection to the server open while the model generates.
  • When streaming fails, send() and close() throw. If the server refuses the stream, or cannot be reached for ten seconds, they throw an IQStream\Exceptions\IQError with the server's status and error code, and so does every call after it. The client has already told the browser that the stream failed, when the server could be reached. It never writes to a log: catch the error where you decide what a failed stream means for your app, for example to log it and let the agent finish its answer without streaming.
  • Long tool calls are fine. When the agent is silent for longer than the server waits (a minute by default), the next send() picks the stream up again, without losing anything. A silence of more than about three minutes closes the stream, unless your loop calls $publisher->keepalive() from time to time.
  • A short answer's close() waits up to three seconds, so that a browser that subscribes just after a fast answer still receives it. new PublisherOptions(minOpen: 0) skips that.
  • If the job fails before it closes the stream, close it with fail from the failure handler, so that the browser hears of it at once: $iq->closeStream(new StreamId($streamId)). A publisher that is dropped without close() does the same.
  • A whole generator in one call: $iq->publish(new StreamId($streamId), $chunks) sends every string it yields and closes the stream, with fail if it throws.
  • A server with your company's own certificate authority: IQClient::fromEnvironment(tls: new TlsOptions(caFile: '/path/to/ca.pem')).

License

MIT