An example chat application used to demonstrate the functionalities of Node.js combined with the Expressjs framework and Socket.io.
- Install Node.js from www.nodejs.org
- In the commandline: npm install -g nodemon
- Install Node.js from www.nodejs.org
- In the terminal: sudo npm install -g nodemon
- Open up the terminal of commandline
- Run the command: node -v
- Run the command: npm -v
- Both of these commands should return a version number
- Download the source from www.vbremer.nl/mtnw.zip
- Extract the contents to a folder
- Start your terminal, and find the files you just extracted
- Run the command: npm install
- Start the server by running: nodemon app.js
- Check if the server is running by pointing your webbrowser to http://localhost:1337
// send to current request socket client
socket.emit('newMessage', "you are connected");
// sending to all clients, include sender
io.sockets.emit('newMessage', "this is a test");
// sending to all clients except sender
socket.broadcast.emit('newMessage', "a new user connected");
// sending to all clients in 'game' room(channel) except sender
socket.broadcast.to('game').emit('newMessage', 'a new user joined the game room');
// sending to all clients in 'game' room(channel), include sender
io.sockets.in('game').emit('newMessage', 'hello gamers');
// sending to individual socketid
io.sockets.socket(socketid).emit('newMessage', 'only for you lady');