Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8737495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:32:07+00:00 2026-06-13T10:32:07+00:00

I want to use node.js with my Rails-Project to serve asynchronous io. I don’t

  • 0

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 with web-socket, server-sent events and spdy without an alternative. My first try to use node.js is now to use Socket.io, just to get in use with serving data to a node.js-module with JavaScript. But it doesn’t 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's 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 get correctly loaded in my application.html.erb:

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

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

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

The message gets created in the ArticlesCrontroller this way:

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 at this event in an other view called index.js.erb this way:

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

The 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 seems to work right, because it tells me right how it serves the requested js-File:

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

But it doesn’t work at all, although the create.js.erb gets rendered without an error:

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

Even the jQuery-Function for resetting the form gets not executed, what works without the try to emit the socket-event. The article gets saved to the database correctly, so that isn’t the problem. Can someone tell me where the problem might be?

Update: I figured out by using the JavaScript-debugger of chrome, that the problem starts with the definition of window.socket in the application.js. The error-code is:

Uncaught ReferenceError: io is not defined
  (anonymous function)

But io is defined in the socket.io.js-File I load in my application.html.erb. I did a workaround for application.js this way:

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

The question is now: Why do I have to get the script this way, although it is loadet in the application.html.erb? But despite this workaround the create.js.erb still doesn’t work yet. I will try to workaround that after having a bit of sleep.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T10:32:09+00:00Added an answer on June 13, 2026 at 10:32 am

    I generally include dependencies in the relevant manifest (in this case, application.js).

    Rails has awareness of many different asset paths, all of which are checked prior to compilation. I’d be inclined to put (e.g. socket.io.js) in vendor/assets/javascripts and add an extra line to the manifest as follows:

    //= require jquery
    //= require jquery_ujs
    //= require socket.io
    //= require_tree .
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use BYTE_ORDER macro in my Xcode project but i can't because i
I'm trying to compile LESS from PHP and don't want to use node.js or
I want to use node.js in my next project, but my boss does not
For web development I'd like to mix rails and node.js since I want to
I want to use Node.js run as a back-end server to serve front-end socket.io
I have a standard node.js static file server that I want to use to
I want use this 1 for using Bar code or QR code scanner. I
I want use jQuery in my project. I know the javascript_include_tag calls the jQuery
I don't want use old Visual Basic methods in my code, and I'm confused
I want to use ._get_type ( node ) function from jsTree plugin types The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.