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

  • Home
  • SEARCH
  • 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 8099045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:16:25+00:00 2026-06-05T22:16:25+00:00

I am playing around with node.js and socket.io-client. I am trying to connect to

  • 0

I am playing around with node.js and socket.io-client. I am trying to connect to a channel that does not exist in order to trigger the event ‘connect_failed’ (as specified at https://github.com/LearnBoost/socket.io-client ).

However I can’t get the event working:

var clientio = require('socket.io-client');
console.log('Trying stuff ...');

// the channel does not exist
var socket = clientio.connect( 'http://localhost:4000/news' );

// I expect this event to be triggered
socket.on('connect_error', function(){
    console.log('Connection Failed');
});
socket.on('connect', function(){
    console.log('Connected');
});
socket.on('disconnect', function () {
  console.log('Disconnected');
});
socket.send('hi there');

If I execute will this will happen:

$ node tmp_clientio.js 
Trying stuff ...

Any ideas about how to trigger an error if connecting to a channel that does not exist?

UPDATE: Renamed connect_failed to connect_error

  • 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-05T22:16:27+00:00Added an answer on June 5, 2026 at 10:16 pm

    I ran into the same issue. There is a un-documented difference between namespace mode on and off that I also ran into, in the end I had to use the chrome debugger to work it out.

    // Bind to the news namespace, also get the underlying socket
    var ns_news = clientio.connect( 'http://localhost:4000/news' );
    var socket = ns_news.socket
    

    So, you see above that you have to get the actual socket ns_news.socket from your namespace if you want to bind “socket level” events such as connect failed, disconnect, etc

    // Global events are bound against socket
    socket.on('connect_failed', function(){
        console.log('Connection Failed');
    });
    socket.on('connect', function(){
        console.log('Connected');
    });
    socket.on('disconnect', function () {
      console.log('Disconnected');
    });
    
    
    // Your events are bound against your namespace(s)
    ns_news.on('myevent', function() {
        // Custom event code here
    });
    

    Note that you now use ns_news to send or receive events

    ns_news.send('hi there');
    

    Finally, be sure to bind the socket.on('error', ...) event as well – it occurs if the port wasn’t available

    Full implimentation of 8080 connect with fallback to port 80

    I did this in my implementation (try port 8080, if that fails, try port 80 through nginx)

    socket_port = 8080;
    
    ns_dash = io.connect("http://your.gridspy.co.nz/dash", {
      port: socket_port,
      'connect timeout': 5000,
      'flash policy port': 10843
    });
    
    socket = ns_dash.socket;
    
    try_other_port = function() {
      if (socket_port !== 80) {
        if (typeof console !== "undefined" && console !== null) {
          console.log("Trying other port, instead of port " + socket_port + ", attempting port 80");
        }
        socket_port = 80;
        socket.options.port = socket_port;
        socket.options.transports = ['htmlfile', 'xhr-multipart', 'xhr-polling', 'jsonp-polling'];
        return socket.connect();
      } else {
        return typeof console !== "undefined" && console !== null ? console.log("No other ports to try.") : void 0;
      }
    };
    socket.on('connect_failed', function() {
      if (typeof console !== "undefined" && console !== null) {
        console.log("Connect failed (port " + socket_port + ")");
      }
      return try_other_port();
    });
    socket.on('error', function() {
      if (typeof console !== "undefined" && console !== null) {
        console.log("Socket.io reported a generic error");
      }
      return try_other_port();
    });
    

    I also added an nginx proxy_pass rule, which looks like this

    location ~ ^/(socket.io)/ {
        proxy_pass http://127.0.0.1:8080;
        proxy_http_version 1.1;
        proxy_set_header Host $http_host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
    

    The Upgrade and Connection options here (from more recent versions of nginx) are required to upgrade the connection between nginx and socket.io to support websockets. That means that your webserver can support websockets on port 80.

    Furthermore if your server supports ssl / https, this will proxy wss which is the secure version of websockets.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While playing around with the emulator, I noticed that when trying to view a
I'm playing around with Journey on node and I only realized that the router
I'm playing around with node.js, trying to re-write a particularly poorly designed part of
I've been exploring javascript more deeply lately, playing around with a node,redis,socket.io,express simple webapp.
Playing around with MongoDB and NoRM in .NET. Thing that confused me - there
I've been playing around with node.js (nodejs) for the past few day and it
I have been playing around with the youtube API and node.js, so far I
I've been playing around with GEdit's syntax highlighting. I love the way that Visual
Just playing around with java trying to learn it etc. Here is my code
So I am developing (more playing around with) a realtime game in node.js, I

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.