
Streaming AI Responses in Rails — ActionCable + Turbo + OpenAI Streaming
This is post #20 in the Ruby for AI series. Last time we built AI agents with tool use. Today we're solving a UX problem: nobody wants to stare at a spinner for 15 seconds while GPT thinks. We're building real-time streaming from OpenAI through ActionCable and Turbo Streams — token by token, as they arrive. The Problem Standard HTTP request-response doesn't work for AI. The model generates tokens over 5-20 seconds. Without streaming, your user sees nothing until the entire response is ready. That's unacceptable. The fix: stream tokens from OpenAI → your Rails server → the browser, in real time. Architecture Here's the flow: OpenAI API (SSE) → Rails Background Job → ActionCable → Turbo Stream → Browser DOM The user submits a prompt. A background job calls OpenAI with stream: true . Each chunk gets broadcast via ActionCable. Turbo Streams update the DOM. No custom JavaScript required. Setting Up the Channel Generate an ActionCable channel for chat streaming: rails generate channel ChatSt
Continue reading on Dev.to
Opens in a new tab



