Urgent.News

What's breaking now, across thousands of outlets.

AI

Building a Streaming Chatbot with Node.js

Most backend tutorials show synchronous calls: the API generates the full response and only then prints it, after several seconds of waiting. That’s not how real chat products work — in interfaces like ChatGPT or Claude, the response starts appearing word by word, before it’s even complete. That’s called streaming , and it’s what we’re going to build today. What you’ll learn Rendering LLM…

Streamed chatbots are an advanced topic, enabling a natural user experience. This tutorial demonstrates building one using Node.js.

Traditional tutorials illustrate synchronous calls, where the API returns the entire response before any output is displayed. Chat interfaces like ChatGPT or Claude, however, display responses incrementally, word by word, before the full answer is complete. This is known as streaming, and the tutorial will implement it.

Key takeaways include rendering LLM responses token by token in the console, understanding the streaming architecture, and creating a reusable pattern applicable across various providers. The tutorial assumes Node.js 18 or higher, npm, and a free API key from Groq.

To begin, a project directory called "llm-chatbot-tutorial" is created, and the necessary npm packages (openai and dotenv) are installed. Credentials are safeguarded by setting up a .gitignore file and a .env.example. The actual .env file contains the real Groq API key.

The core chatbot code resides in chatbot.js. It imports the dotenv package to load environment variables, sets up the OpenAI client with Groq's API endpoint, and specifies the GPT-oss-120b model. The chat function handles user input, logs the user message, and sends it to the API with streaming enabled.

The for await loop iterates over each fragment of the response as it arrives. Instead of console.log, process.stdout.write is used to avoid additional line breaks, preserving the real-time typing effect. The chatbot is then tested with various queries, demonstrating the streaming capability in action.

Potential errors are addressed, such as checking the .env file location, verifying the API key, and confirming the model's availability. Finally, the tutorial encourages customization by modifying the chat(...) calls and adding a system prompt for controlling the assistant's behavior.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Also reported by 1 other outlet

Read the original at dev.to →

More in AI

Gemma 4 on a Tesla T4: QAT Weights Decode 1.79x Faster Than bf16

This article provides a step by step deployment guide for **Gemma 4 E2B * to a Tesla T4 hosted GPU enabled system. A suite of Python MCP tools is built to simplify management of the vLLM hosted…

  • Gemma 4 E2B deployed on Tesla T4 GPU
  • Deployment uses Python MCP tools
  • Gemma 4 decodes 1.79x faster than bf16

More from Friday 18 September →