WebSockets — Complete Guide
WebSockets — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of React.js Tutorial on Toolliyo Academy.
On this page
React.js Tutorial · Lesson 73 of 100
SSE
Beginner ✓ → Intermediate ✓ → Advanced ✓ → Professional
Professional · 4 — Real projects · ~25 min read · Module 8: Real-Time & Full-Stack React
Introduction
Professional project lesson: SSE. You will put together routing, data, and UI like a portfolio app. Build one piece at a time — do not rush. Server-Sent Events (SSE) stream text events from server to browser over HTTP. Browser API: EventSource. Simpler than WebSockets when you only need server push — live logs, stock ticks, notifications.
Frontend and backend are separate programs that talk over HTTP. React is the frontend; your API is the backend.
When will you use this?
Use when your React app must talk to a real backend or show live updates.
- React frontends talk to .NET, Node, or Java APIs — fetch and auth are daily tasks.
- Live chat and stock tickers use WebSockets or SignalR with React state.
Real-world: stream deploy logs
DevOps UI uses Server-Sent Events to stream CI log lines to React terminal component — simpler than WebSocket for one-way feed.
Production-style code
function LogStream({ buildId }) {
const [lines, setLines] = useState([]);
useEffect(() => {
const es = new EventSource(`/api/builds/${buildId}/logs/stream`);
es.onmessage = e => setLines(prev => [...prev, e.data]);
es.onerror = () => es.close();
return () => es.close();
}, [buildId]);
return (
<pre>{lines.join('\n')}</pre>
);
}
What happens in production: SSE works over HTTP/2, auto-reconnects — perfect for log tail and notification feeds.
Lesson example (start here)
Copy this smaller example first. Once it works, compare it with the real-world code above.
useEffect(() => {
const es = new EventSource('/api/stream');
es.onmessage = e => setTick(JSON.parse(e.data));
es.onerror = () => es.close();
return () => es.close();
}, []);
Line-by-line walkthrough
| Code | What it means |
|---|---|
useEffect(() => { | Runs side effects — fetch data, timers, or subscriptions — after the screen updates. |
const es = new EventSource('/api/stream'); | Part of the SSE example — read it together with the lines before and after. |
es.onmessage = e => setTick(JSON.parse(e.data)); | Defines a function — often a component or event handler. |
es.onerror = () => es.close(); | Defines a function — often a component or event handler. |
return () => es.close(); | Returns JSX — the HTML-like UI this component displays. |
}, []); | Closes a block started by { or ( above. |
How it works (big picture)
- Server sends data: lines.
- EventSource auto-reconnects.
- One-way only — client still uses fetch for commands.
Do this on your computer
- Backend endpoint with text/event-stream content type.
- Create EventSource in useEffect.
- Update UI on each message.
- Read the real-world section and name which part of the app uses this topic.
- Run the example locally and confirm the same behavior in the browser.
- Change one value in the example (text, initial state, or URL) and predict what will happen before you save.
Experiments — try changing this
- Change text or labels in the example and save — watch the browser update.
- Break the code on purpose (remove a bracket), read the error message, then fix it.
Remember
SSE = one-way server push over HTTP. EventSource in React useEffect. Good for feeds and live updates.
Common questions
SSE through proxies?
Generally works better than WebSockets through some corporate proxies.
How long should I spend on SSE?
Until you can explain it in your own words and run the example without looking at the answer. Beginners often need 30–60 minutes per new hook or routing topic; setup lessons may take one afternoon.
What if I get stuck on SSE?
Re-read the line-by-line walkthrough, check the browser console for red errors, and compare your code character-by-character with the example. Search the exact error text — someone else had it too.
Where is SSE used in real jobs?
See the real-world section above — the same pattern appears in LMS, banking, e-commerce, and SaaS products. Interviewers ask you to explain it using one concrete example from your project or this lesson.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!