SShortSingh.
Back to feed

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

0
·1 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 ·

Brazilian Lab Trains 144M-Parameter Decision AI Model for Just $104

Supersonic Labs, a small AI lab in Brazil, has released Julia 1, a 144.3-million-parameter decision model trained at a total cost of approximately US$104. Unlike generative language models, Julia 1 does not produce text — instead, it accepts a context, a question, and up to 20 options, then returns ranked scores for each choice. The model runs on consumer hardware including Android tablets and budget laptop CPUs, with weights of 550 MB fitting within 400 MB of RAM. In benchmark testing, Julia 1 outperformed comparable models on emotion classification by 38 percentage points, though it struggled with large label sets such as Banking77's 72 categories. Released on Hugging Face under the Apache 2.0 license, the model also achieved over 71% accuracy across 52 language locales in multilingual scenario classification.

0
ProgrammingDEV Community ·

How PayEcho Forced Its LLM to Act on Customer History, Not Ignore It

Engineers at PayEcho, a payment-recovery and credit-decision platform, found that integrating recalled customer history into an LLM's context was not enough to change its recommendations. The model consistently produced generic advice even when months of behavioral data were available, treating historical information as background rather than evidence. The team solved this by requiring the model to explicitly cite specific past outcomes — such as a customer ignoring emails but responding to WhatsApp — as justification for each recommendation. Retrieval and generation were kept as separate pipeline stages, making it easier to diagnose whether a generic output stemmed from poor memory recall or failure to reason over retrieved data. Actual outcomes from each interaction are written back to memory, creating a feedback loop where past results become evidence for future decisions.

0
ProgrammingDEV Community ·

College Game Dev Club Builds Mobile-Friendly 3D Web Game Using Three.js

A college game development club built a browser-based 3D exploration game using Three.js, TypeScript, Cannon.es, and GLSL shaders, distributable via a simple QR code. The game features a car navigating an island with a mini-game and information zones, inspired by developer Bruno Simon's interactive portfolio. To ensure smooth performance on mobile devices, the team used instanced geometry and custom GPU shaders to render 15,000 grass blades, procedural flowers, butterflies, and dolphins while minimizing CPU load. Expensive post-processing effects like SSAO and Bloom were replaced with lightweight alternatives, including darkened grass bases to simulate ambient occlusion and CSS filters for color grading. The project was developed in three phases: establishing driving controls with primitives, integrating 3D models, and finally refining visuals while optimizing for mobile browsers.

0
ProgrammingDEV Community ·

NanoAgent PHP Library Adds Persistent Chat Memory with Swappable Storage Drivers

NanoAgent, a PHP agent library, has introduced a built-in memory system that allows chatbot conversation history to persist across server restarts and browser sessions. Developers can attach a memory driver to an agent using a single setMemory() call, which automatically loads prior history and saves it after every chat interaction. The library offers three drivers: ArrayMemory for in-process-only storage, FileMemory for single-user file-based persistence, and PdoMemory for SQL-backed multi-user production environments supporting SQLite, MySQL, and PostgreSQL. FileMemory uses hashed session IDs and atomic file writes to prevent directory traversal and data corruption from crashed requests. Resetting a session's history is handled through a clearHistory() method that removes both in-memory and stored data in one step.