views:

258

answers:

4

What is the best way to implement real time updates in ruby on rails using node.js? It would be great to hear either real examples or your thoughts on alternative solutions.

A: 

The way to do it would be with a javascript framework like jquery. Once rails renders the views and passes the html to the client's browser, the javascript can take over to handle updates and request information from node.js since node can handle thousands of concurrent connections.

You can use this method for simple ajax calls or form more complex comet push updates.

Seth Archer
A: 

It's not that simple, I'm afraid...

cause we got the same origin policy in that case, the page (rendered by rails) and the update server (node) must be on the same server & port

I still don't know how to do this.

see my answer :)
Sam Saffron
Hi sam, thanks for the answer...one more question, can we use nginx if we want to use websocket?thanks
apparently not http://stackoverflow.com/questions/2419346/can-nginx-be-used-as-a-reverse-proxy-for-a-backend-websocket-server but you could use haproxy
Sam Saffron
+2  A: 

I use node for chat with a rails app.

The way I do it is by setting up an nginx front end that proxies my Rails app and my node app.

This allows you to get around the same origin policy and cross communicate.

Here is a snippet of my nginx.conf

   location /chat_service {
      rewrite      /chat_service/(.+) /$1 break;
      proxy_pass http://localhost:9000/;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header Host $host;
    }

This means that I can render the html pages from my rails app and communicate with the node app without having to use nasty hacks like JSONP.

A full example is way out of the scope of this answer, but with a good proxy in front you can get them to work happily together.

Sam Saffron
A: 

Why don't you use Socket.io ?