Rails 3.1 - JS - Socket.io-emit in * .js.erb does not start and prevents jQuery-Function from executing

I want to use node.js with my Rails-Project to serve asynchronous io. I don't want to use juggernaut, faye or something like this because I need clean connections to web sockets, events sent by the server, and spdy without an alternative. My first attempt to use node.js is now to use Socket.io to just use the service data in the node.js module with JavaScript. But this does not work at all.

My application.js looks like this:

// This is a manifest file that'll be compiled into including all the files listed below. // Add new JavaScript/Coffee code in separate files in this directory and they'll automatically // be included in the compiled file accessible from http://example.com/assets/application.js // It not advisable to add code directly here, but if you do, it'll appear at the bottom of the // the compiled file. // //= require jquery //= require jquery_ujs //= require_tree . window.socket = io.connect('http://localhost:8080/'); 

The requirements for io load correctly in my application.html.erb application:

  <%= javascript_include_tag "application", "http://localhost:8080/socket.io/socket.io.js" %> 

Now I want to use a socket to fire an event sent to all listening clients, like this in create.js.erb:

 $(function() { window.socket.emit('message', <%= @message =>); }; $("#new_article")[0].reset(); 

A message is created in ArticleCrontroller as follows:

 def create @article = current_user.articles.build(params[:article]) if @article.save msg = {:data => @article.to_json} @message = msg.to_json end end 

I listen to this event in another view called index.js.erb as follows:

 $(function() { window.socket.on('message', function (msg) { $("#articles").prepend(<%= escape_javascript render(Article.new.from_json(msg.data)) %>); }); }); 

Socket.io looks like this:

 var io = require('socket.io').listen(8080); io.sockets.on('connection', function (socket) { socket.on('message', function () { }); socket.on('disconnect', function () { }); }); 

and it seems to work correctly, because it tells me how it serves the requested js file:

 info - socket.io started debug - served static /socket.io.js 

But this does not work at all, although create.js.erb is obtained without errors:

 Rendered articles/create.js.erb (0.5ms) Completed 200 OK in 268ms (Views: 70.0ms | ActiveRecord: 14.6ms) 

Even the jQuery function to reset the form fails, which works without trying to emit a socket event. The article is stored correctly in the database, so this is not a problem. Can someone tell me where the problem might be?

Update: I realized, using the chrome JavaScript debugger, that the problem starts with defining window.socket in application.js. Error code:

 Uncaught ReferenceError: io is not defined (anonymous function) 

But io is defined in socket.io.js-File, which is loaded in my application.html.erb application. I made a workaround for application.js as follows:

 $.getScript('http://localhost:8080/socket.io/socket.io.js', function(){ window.socket = io.connect('http://localhost:8080/'); }); 

The question is, why should I get the script this way, although this is loadet in application.html.erb? But despite this, create.js.erb still does not work. I will try a workaround after a short sleep.

+4
source share
1 answer

I usually include dependencies in the corresponding manifest (in this case application.js ).

Rails has an understanding of many asset paths, all of which are verified before compilation. I would be inclined to put (e.g. socket.io.js ) in vendor/assets/javascripts and add an extra line to the manifest like this:

 //= require jquery //= require jquery_ujs //= require socket.io //= require_tree . 
+1
source

All Articles