The recent need to build a scalable chat room environment, daoteng a half-day, sorting out the plan. This solution meets the following requirements:
⑴ based on websocket communication.
⑵ node. JS server runs in the background as a deamon process.
⑶ the socket server can be scaled horizontally by increasing the server.
Small Series I engage in technology, work to direct hit the key, concise, so the main steps explained the next. For more information, see "References" at the bottom of the article.
1. Installing nodejs
⑴ preparatory work
Yum-y install gcc make gcc-c++ openssl-devel wget
⑵ Download & unzip
CD/ opt /src
wget http://nodejs.org/dist/v0.10. Ten / node-v0.12.10.tar.gz
TAR-ZVXF node-v0.12.10.tar.gz
⑶ Compiling the installation
./configure
./ Make && make install
⑷ Verify that the installation configuration is successful
Node-v
2. use node.js+socket.io to build WebSocket Real-time applications
⑴ Execute the following command under the /var/www/html/ping directory
NPM Install--save EXPRESSNPM
Install--save Socket.io
After the installation is successful, you should see a folder named Node_modules in the working directory, with Express and socket.io , respectively . , you can then start writing the service-side code, creating a new file: index.js:
var app = require (' Express ') ();
var http = require (' http '). Server (APP);
var io = require (' Socket.io ') (HTTP);
App.get ('/', function (req, res) {
Res.send ('
});
Http.listen (, function () {
Console.log (' listening on *:3000 ');
});
⑵ run Nodejs
command line run node index.js, if all goes well, you should see the listening on *:3000 returned , which indicates the service has been successfully built. You should see the normal welcome page when you open http://localhost:3000 in your browser.
If you want to let the service run online server, and can be accessed through the domain name, you can use Nginx Proxy, Add the following configuration in nginx.conf, and then the domain name (such as: realtime.plhwin.com) resolves to the server IP .
Server
{
Listen 80;
server_name realtime.plhwin.com;
Location/{
Proxy_pass http://127.0.0.1:3000;
}
}
3. Building the Websocket Server cluster
Use the ngix reverse proxy scheme. This scheme is relatively simple and does not need to modify the business code can be directly deployment, through the iphash rotation algorithm to save the user assigned to the same process.
Vi/etc/nginx/conf/nginx.conf
HTTP {
Upstream Io_nodes {
Ip_hash;
Server 127.0.0.1:6001;
Server 127.0.0.1:6002;
Server 127.0.0.1:6003;
Server 127.0.0.1:6004;
}
server {
Listen 3000;
server_name io.yourhost.com;
Location/{
# to support forwarding WebSocket data, plus upgrade Header
Proxy_set_header Upgrade $http _upgrade;
Proxy_set_header Connection "Upgrade";
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
Proxy_set_header Host $host;
Proxy_http_version 1.1;
Proxy_pass Http://io_nodes;
}
}
}
4. Forever let node. js Run in the background
⑴ Installing Forever
NPM Install FOREVER-GD
⑵ run Forever
Forever--help
Forever Start Server.js
Forever Stop Server.js
Reference:
1. "Centos installation NodeJS"
Http://www.cnblogs.com/hamy/p/3632574.html
2. "centos6.5 compiled and installed nodejs"
Http://blog.sina.com.cn/s/blog_6722ab160102vtvb.html
3. "Use node.js+socket.io to build WebSocket Real-time application"
http://www.plhwin.com/2014/05/28/nodejs-socketio/
5. the cluster programme of thesocket.io
Http://www.open-open.com/lib/view/open1447034396335.html
6. node. js App daemon manager Forever installation and use instance
Http://www.jb51.net/article/50543.htm
This article is from the "Programming Art" blog, so be sure to keep this source http://itsart.blog.51cto.com/1005243/1772493
node. js + Nginx builds a websocket-based, extensible Message Center