This chatRoom is based on the “express”. Make sure you have installed express. If not try
npm install express
This URL is a useful link for express website.
Then Let’s make a express project, express chatRoom. Then, initialize that project.
If, npm install is slow, do not worry, it’s slow with me as well XD..However, if it occurs with some error, please try
sudo npm installnode app. When you type localhost:3000/ in your browser, you will find that the website will occur some error, like that
In order to fix this problem, you only need to change
/views/layout.jade‘s first line, doctype 5 => doctype html. Then, run again:-). Now, you have an express project.
Express framework works fine, we then need to install socket.io
npm install socket.io --save. The reason why I write the “–save” is it will save it to “package.json” as an dependency. socket.io is prepared, write some code to the app.js.
The initial express project will have
Then set that function as a variable called server. Because we need to use this server later. write
var io = require('socket.io')(server);
to include socket.io to the server, and then
Then refresh the browser, you can find that the console will show “Client connected…” often;-) That means we have connected with socket.ioXD
Then, we need to send message’s.
all socket.io’s emit request means need the request to the server, so first it need a user to type his/her nickname, then we store that value in the server.
Here is what we do with the server:
The reason why we need both
client.emit is both me and other people in the chatroom need to receive what we send. Then, let’s enjoy the chatroom you create. XD In the next article I am going to show some demo about how to create one to one chat.
This is the GitHub project, if you have any problem check it: