Rails AJAX slow compared to Node.js and websockets complex to implement in rails ?

I have heard alot about Node.js and watched a few tutorials and read some stuff. I get the impression that in certain situations AJAX calls with node.js is alot faster than with rails. I also have somewhat of an impression that with Rails web sockets are complicated to implement but with node.js web sockets might be alot easier.

I am trying to figure out if I should learn some node.js or if it could be a passing fad to be replaced with some other technology soon. I am also wondering what sorts of improvements to rails might be coming in the future that could make AJAX calls faster in some situations as well as easier web socket implementations ? Are there things out there already ? Does it make sense to run a sinatra app along with rails for faster AJAX ?

How can a websocket implementation work easily with rails when it is different than the typical request cycle of HTTP ?

Otherwise I guess alot of AJAX stuff is just based on polling if the server wants to refresh page content without any user triggered events ?

I did an experimental web socket implementation using ruby faye a couple of years ago that needed event machine. I did think it was a bit complicated.

Have you read this: http://greenash.net.au/thoughts/2012/11/nodejs-itself-is-blocking-only-its-io-is-non-blocking/

Its about having a non-blocking system which helps in increasing speed of execution… it not about the language. Depends on whether your AJAX calls are calling blocking systems, or non-blocking ones.

Learn node.js. It does not seem to be a passing fad. Rails is very much alive and kicking. Also check out Java/Scala Play. Then see how the dots all connect.

Gurpreet’s response is accurate, but I think I would add a few things. I know Rails pretty well, but I’m just learning node.js. while it’s true that you can write blocking (synchronous) or non-blocking (asynchronous) code in either language, Ruby is primarily a blocking language while Javascript is primarily non-blocking. As an example, both languages have relatively simple statements to open a file and read the contents of the file:

[Open file statement]

[Read file contents statement]

By default, if you run this in Ruby, you will get what you expect. It will open the file, then read its contents. Javascript won’t. It will issue the command to the operating system to open the file, then attempt to read the contents whether the file has finished opening or not. In javascript, you have to program the second statement as a function callback to the first statement.

You can write this asynchronously in Ruby, but that’s not the default. Simlarly, there’s are opensync and readsync functions in the javascript fs library which will operate synchronously, but you have to consciously use them.

Therefore, I still find node.js easier to use for asynchronous chaining. However, it would not meet my needs for an overall web application development framework. It is nowhere near as strong as rails (e.g., routing in node.js is much more work), even with frameworks such as express.js and templating engines. The other thing you have to watch is that there is absolutely no concurrency in node.js and you wouldn’t want to use it for heavy calculations. Rails also has much broader support for all kinds of things through gems. The npm library (the node.js equivalent) is nowhere close to being as robust and it is, again, a pain. Managing dependencies with the Ruby bundler is way easier than node.js. Convention is much more established in Rails, where node.js requires configuration.

My goal is to be able to use the two together (I’m not close to that yet). I would like to do web application development in Rails and use node.js for Ajax calls, particularly straightforward calls such as basic create, update, and delete operations and things like web socket pushing of information in json format.

With respect to web sockets, I don’t work with it much because support on Internet Explorer and Android is relatively recent and older versions of both browsers are still in use with a considerable number of users. My applications also typically don’t have a need for that type of push communication. I can tell you that node.js does have fairly robust support through socket.IO. The nice thing about socket.IO is that it kind of abstracts the bidirectional communication issue. It tests for websocket support. If it doesn’t exist, it uses older polling methods. You don’t really have to worry about it. To be honest, I haven’t really pursued web socket support in Ruby so I’m not sure what the options are or how complicated it is.

sorry if this was a little long, but I hope it answers your question.

just some bikeshedding … sry - your answers are all very good!

I stand corrected, you are quite right. node.js is built on a version of Chrome’s javascript runtime engine so that it’s core language is identical to javascript. Javascript is, primarily, asynchronous and therefore, node.js, is by inheritance. However, the example I gave is clearly node.js, not javascript.

There’s another correction. The penultimate sentence of the fourth paragraph should read, "Managing dependencies with the Ruby bundler is way easier than npm (not node.js).

Thanks,

Mike