SShortSingh.
Back to feed

How to Stream LLM Tokens in PHP Using Server-Sent Events

0
·58 views

Developers building chat interfaces in PHP can eliminate the blank-screen wait by streaming large language model (LLM) tokens incrementally using Server-Sent Events (SSE). The NanoAgent library's Agent::stream() method delivers each token via a callback, which the developer formats as an SSE data line and immediately flushes to the browser. Four specific HTTP headers are required to convert a standard PHP response into a persistent SSE stream, with the X-Accel-Buffering header being critical for Nginx environments that buffer upstream responses by default. On the client side, a native browser EventSource object handles incoming tokens without any additional libraries, appending each token to the UI until a [DONE] sentinel signals the end of the stream. Compared to WebSockets, SSE is simpler for one-way token delivery since it runs over plain HTTP and supports automatic reconnection.

Read the full story at DEV Community

This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)

Log in to join the discussion and vote.

Log in

Related stories

0
ProgrammingDEV Community ·

Glimpse Developer Adds Live Note-Taking to Meeting App After Using It in Lectures

A developer spent a year building Glimpse, a dictation and meeting app, largely on their own. After matching the features users expect from dictation tools, they focused on meeting mode, drawing inspiration from MacWhisper. When MacWhisper launched live transcripts, the developer added a similar feature to Glimpse. Using it during lectures revealed shortcomings, such as the app listening for too long or too short a time and not indicating which model was processing the live notes. The developer's takeaways are to copy what users expect, scrutinize what you dislike even in copied features, and use your own product constantly to catch problems before others do.

0
ProgrammingDEV Community ·

OAB Sets Guardrails Requiring Human Lawyer Oversight of AI in Legal Services

A DEV Community article outlines technical and ethical guardrails meant to limit how artificial intelligence is used in Brazilian legal work. It says the Brazilian Bar Association (OAB) requires that any AI-assisted legal output be directly supervised and signed off by a registered lawyer, and that responsibility for AI-driven errors rests with the lawyer rather than the technology provider. The piece cites the Estatuto da Advocacia, OAB opinions and resolutions, and reported court practice, noting courts tend to reject filings whose authorship cannot be clearly attributed to a qualified professional. Suggested safeguards include blocking unsigned AI outputs, mandatory warnings on sensitive topics, immutable logs of AI interactions, and disabling automated drafting of complex documents. The article presents these as current expectations in the profession rather than a new regulatory announcement.

0
ProgrammingDEV Community ·

Setting Up External DNS with Cloudflare on a Local Kubernetes Cluster

This installment of a Kubernetes series explains how to deploy External DNS to automate DNS record management on a local cluster. The author uses Cloudflare as the DNS provider and follows declarative GitOps practices with Flux. The process starts by generating a zone-scoped Cloudflare API token and storing its credentials in a Kubernetes secret, which is encrypted with SOPS before being committed to Git. The External DNS deployment is then defined through a ServiceAccount, a read-only ClusterRole and binding, and a Deployment that watches ingresses and syncs records to Cloudflare. The article notes that the setup builds on earlier steps for routing external traffic to the cluster so it can be reached via a public IP.

0
ProgrammingDEV Community ·

DEV community asks which single fix users would make to the platform

A DEV Community post invites readers to name the one thing they would fix on the platform if given a magic wand. The author asks for a single answer, which can be a feature, rule, bug, or aspect of how the feed works. The author gives an example: a way to understand why a post received few views, which would especially help new writers who get no initial readers. The post notes that, without such insight, writers can only guess at the reason. The author states they will read every reply.