Building a Real-Time Chat Component
Message list with v-for, input binding, emit on submit, scroll to bottom on new message.
Building a Real-Time Chat Component is a free Vue Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Vue Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Goal: A Live Chat Box
We will assemble a small but complete chat component: it shows a scrolling list of messages, lets the user type and send, and appends incoming messages in real time.
This ties together reactive state, lifecycle hooks, and DOM access.
The Messages Ref
Hold the conversation in a reactive array. Each entry is a simple object with an id, author, and text.
import { ref } from "vue"
const messages = ref([
{ id: 1, user: "system", text: "Welcome to the room" },
])A Ref for the Input
Bind the text box to a separate ref with v-model. This holds the message being composed before it is sent.
// template:
// <input v-model="draft" @keyup.enter="sendMessage" />
const draft = ref("")Registering the Socket Listener
In onMounted, subscribe to incoming messages and push each into the array. Using onMounted guarantees the component is ready before we attach handlers.
import { onMounted } from "vue"
onMounted(() => {
socket.on("message", (msg) => {
messages.value.push(msg)
})
})Sending a Message
The sendMessage function emits the draft to the server and clears the input. We guard against sending empty strings.
function sendMessage() {
const text = draft.value.trim()
if (!text) return
socket.emit("send", { text })
draft.value = ""
}Rendering the List
The template loops over messages with v-for, keyed by id. Each row shows the author and text.
<!-- template -->
<div ref="chatBox" class="chat">
<p v-for="m in messages" :key="m.id">
<strong>{{ m.user }}:</strong> {{ m.text }}
</p>
</div>The Scrolling Problem
When a new message arrives, the user wants the view pinned to the bottom. But if you scroll right after pushing to the array, the new row is not in the DOM yet — Vue has not flushed the update.
You scroll too early and miss the latest line.
A Template Ref to the Container
Grab the scroll container with a template ref so you can read and set its scroll position.
import { ref } from "vue"
const chatBox = ref(null)
// template: <div ref="chatBox"> ... </div>Waiting for the DOM with nextTick
nextTick returns a promise that resolves after Vue applies pending DOM updates. Await it before scrolling so the new message exists in the DOM.
import { nextTick } from "vue"
async function scrollToBottom() {
await nextTick()
const el = chatBox.value
el.scrollTop = el.scrollHeight
}Scroll After Every New Message
Call scrollToBottom right after pushing a message — both for incoming and outgoing. Setting scrollTop = scrollHeight jumps to the very bottom.
onMounted(() => {
socket.on("message", async (msg) => {
messages.value.push(msg)
await scrollToBottom()
})
})The Complete Flow
The component now: receives a message, updates reactive state, waits for the DOM via nextTick, then scrolls. Sending mirrors the same path. Clean, predictable, real time.
async function sendMessage() {
const text = draft.value.trim()
if (!text) return
socket.emit("send", { text })
draft.value = ""
await scrollToBottom()
}Quick Check
Check your understanding of DOM timing in the chat component.
Recap
In this lesson you built a real-time chat component. Key points:
- Keep messages in a reactive
refarray and the draft in its own ref. - Register the socket listener in
onMountedand push incoming messages. sendMessageemits the draft and clears the input.- Use a template ref plus
await nextTick()before settingscrollTop = scrollHeightso the newest message is visible.
Frequently asked questions
Is the “Building a Real-Time Chat Component” lesson free?
Yes — the full text of “Building a Real-Time Chat Component” is free to read here on the web, and the Vue Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Vue Academy course, upgrade to CoddyKit PRO.
What will I learn in “Building a Real-Time Chat Component”?
Message list with v-for, input binding, emit on submit, scroll to bottom on new message. You practise Vue Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Vue Academy?
No prior experience is required. Vue Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Building a Real-Time Chat Component” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Vue Academy lesson?
Yes. Every Vue Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- WebSocket Basics in the Browser
- Socket.io Client with Vue
- Building a Real-Time Chat Component
- useWebSocket Composable with Reconnection