Change website

From Jan 16 2015,


All post content will be move to we's offical website with many content...

Can access website here: http://justox.com

Thanks for your visit!

Wednesday, 18 December 2013

Nginx Proxy Websockets To Socket.IO

Everyone got excited about new feature in nginx. From version 1.3.13 nginx have native support for proxying websockets, but i can not find it useful on any project of mine, please correct me if i’m wrong, and if you wish you can give me idea in comments. I just want to try it but i spend like 2-3 hours and hardly make them to work.

Don’t hate player, hate the game!
I really don’t expect for every blogger on internet who writes about anything to cover all points so, i will try cover everything here, maybe..
First, you need to install at least nginx version 1.3.13 or above, also, you don’t need compile or do anything else with nginx installation (eg. adding aditional modules or enabling them) but you probably already done that. Next thing is to install node.js. I’m using debian squeeze and what i did is to clone source from git and then compile it (google it..) :
sudo apt-get update && apt-get install git-core curl build-essential openssl libssl-dev
git clone https://github.com/joyent/node.git
cd node
git checkout enter-a-version
# in other tutorial, seems node can not find libssl so we need point to it
./configure --openssl-libpath=/usr/lib/ssl
make
make test
sudo make install
node -v # it's alive!
# npm is package manager for node, and it comes with node installation
npm -v
Now, when we have node.js, we also need socket.io which can be installed from node.js package manager with running next command :
npm install socket.io
Now i think we can go to next step, this is where we can make new conf file for nginx with sudo nano /etc/nginx/sites-available/websocket.conf, search for your own path if different and add fallowing (you can also copy this from nginx.org website, i just made some modifications..extended existing):
upstream backend {
    server 127.0.0.1:8080;
}

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

server {
    listen 80;
    server_name _;
    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;

        proxy_pass http://backend/;
        proxy_redirect off;

        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        access_log off;
        error_log /var/log/nginx/websockets.error.log;
    }
}
From nginx config, you can see that i want to run socket.io app on 8080 port, you can change this, but this was only for my test purpose.. also, you can ask, why the heck you need nginx there because socket.io can run without it? .. well, yes it can, but if you go to production you probably want sockets on port 80 .. guess what? port 80 is already in use, and that’s why you need proxy_pass to background socket.io server app.
Now, i will only paste existing code which i also copied from socket.io site, and i will paste it here just so you don’t need to search in 10 places like me ..
// server.js
// if you receive that socket.io library missing, you can always do # locate socket.io.js and copy path 
var io = require('socket.io').listen(8080);

io.sockets.on('connection', function (socket) {
  socket.on('set nickname', function (name) {
    socket.set('nickname', name, function () {
      socket.emit('ready');
    });
  });

  socket.on('msg', function () {
    socket.get('nickname', function (err, name) {
      console.log('Chat message by ', name);
    });
  });
});
We want client part to, and you should create index.html and paste this ..
<script type="text/javascript" src="/socket.io/socket.io.js"></script>
<script type="text/javascript">
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>
Problem on which i run to is that i forgot to check my rules on simple project i have, so when i tried to run this index.html from my other vhost i got 404 error on socket.io.js .. i’ve searched and looking what i was doing wrong and then i started to look at my other vhost (from which i tried to run websockets), i had rule where i match anything after location /, so actually i never matched socket.io :) .. heck, i hope you will be more careful :)
To run socket.io server without exiting eg. background process, you should run nohup node server.js &.
That’s it, if you have some question, please ask :) Also, i would like to have any comment bellow from you because i want to know for what and how you plan to use websockets :)
Happy hacking!

No comments:

Post a Comment