[Node.js]29. Level 6: Socket.io: Setting up Socket.io server-side & Client socket.io setup
Below we've already created an express server, but we want to start building a real-time Q&A moderation service and we've decided to use socket.io.
Require socket.io and make sure it listens for requests on the express app.
Also, print out a message to the console whenever a new socket.io client connects to the server.
app.js
var express = require('express'); var socket = require('socket.io'); var app = express.createServer(); var io = socket.listen(app); io.sockets.on('connection', function(client){ console.log("Welcome..."); });
In our html
file, load the socket.io.js
script and then use io.connect
to connect to socket.io on the server. Connect to the server at http://localhost:8080
.
Tip: the socket.io.js path you should use is /socket.io/socket.io.js
. Express knows to serve the socket.io client js for this path.
index.html
<script src="/socket.io/socket.io.js"></script> <script> // use the socket.io server to connect to localhost:8080 here var server = io.connect('http://localhost:8080'); </script>
In our client below, listen for 'question'
events from the server and call the insertQuestion
function whenever the event fires. The insertQuestion
function is already created for you, and it's placed in its own file. It expects exactly one argument - the question.
index.html
<script src="/socket.io/socket.io.js" /> <script src="/insertQuestion.js" /> <script> var server = io.connect('http://localhost:8080'); // insert code here server.on('question', function(data){ insertQuestion(data); }); </script>
insertQuestion.js
var insertQuestion = function(question){ var newQuestion = document.createElement('li'); newQuestion.innerHTML = question; var questions = document.getElementsByTagName('ul')[0]; return questions.appendChild(newQuestion); }
When a question is submitted to our server, we want to broadcast it out to all the connected clients so they can have a chance to answer it.
In the server below, listen for 'question'
events from clients and then emit the 'question
' event on all the other clients connected, passing them the question data.
var express = require('express'); var app = express.createServer(); var socket = require('socket.io'); var io = socket.listen(app); io.sockets.on('connection', function(client) { console.log("Client connected..."); // listen here client.on('question', function(question){ //All client, so it is broadcast client.broadcast.emit('question', question); }); });
In our real-time Q&A app, we want to allow each client only 1 question at a time, but how do we enforce this rule?
We can use socket.io's ability to save data on the client, so whenever a question is asked, we first want to check the 'question_asked'
value on the client. If it's not already set to true, broadcast the question and then go ahead and set the value to true.
var express = require('express'); var app = express.createServer(); var socket = require('socket.io'); var io = socket.listen(app); io.sockets.on('connection', function(client) { console.log("Client connected..."); client.on('question', function(question) { client.get('question_asked', function(err, asked){ if(!asked){ client.broadcast.emit('question', question); client.set('question_asked', true); } }); }); });
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具