CentOS 7
Sponsored Link

Install Node.js2015/08/16

Install ServerSide JavaScript Environment "Node.js".
[1] Install Node.js and npm which is the package management tool.
# install from EPEL

[root@www ~]#
yum --enablerepo=epel -y install nodejs npm
[2] Create a test tool. It's possible to do it as a common user.
[cent@www ~]$
vi helloworld.js
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '');
console.log('listening on');

# run server

[cent@www ~]$
node helloworld.js &
# verify (it's OK if following reply is back )

[cent@www ~]$

Hello World
[3] Install Socket.IO and create a test apprication with using WebSocket.
[cent@www ~]$
npm install socket.io express
[cent@www ~]$
vi chat.js
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);

http.listen(1337, function(){
  console.log('listening on *:1337');

[cent@www ~]$
vi index.html
<!DOCTYPE html>
<title>WebSocket Chat</title>
<form action="">
<input id="sendmsg" autocomplete="off" /><button>Send</button>
<ul id="messages" style="list-style-type: decimal; font-size: 16px; font-family: Arial;"></ul>
<script src="/socket.io/socket.io.js"></script>
<script src="http://code.jquery.com/jquery.min.js"></script>
  var socket = io();
    socket.emit('chat message', $('#sendmsg').val());
    return false;
  socket.on('chat message', function(msg){
    $('#messages').append($('<li style="margin-bottom: 5px;">').text(msg));

[cent@www ~]$
node chat.js

listening on *:1337
  Access to "http://(server's hostname or IP address):1337/" from a Client computer to make sure sample app works normally.
Matched Content