2

I know there are a bunch of questions on this already, but none have answered it for me, and plus mine is slightly different.

I'm starting the socket.io server in node using this:

var io = require('socket.io').listen(8000);

My terminal says everything is ok:

info  - socket.io started

Now I am trying to load the .js file in my clientside browser using this url:

http://<hostname>:8000/socket.io/socket.io.js

I dont get a 404, it just hangs forever. I've also used a network utility to ping port 8000, and it seems to be open fine.

I installed node and socket.io just yesterday, so they should be the latest versions. Can anyone shed any light on this? Thanks!

3
  • Have you created the http server? Commented Mar 1, 2013 at 15:32
  • Thats my line of thought, but the socket.io "how to use" says this: // note, io.listen(<port>) will create a http server for you var io = require('socket.io').listen(80); Commented Mar 1, 2013 at 15:36
  • 1
    I see that line. For what I understand, if you load socket.io without the http server, you can be listening for events in that port, but not serving the js file itself. But I'm not sure if I'm right though. Commented Mar 1, 2013 at 15:44

4 Answers 4

2

Turns out the reason I could never request the .js file was because my company network blocks all ports except the usual ones (80, 21, etc), so `I would never be able to communicate with port 8000.

Sign up to request clarification or add additional context in comments.

1 Comment

omg thank you. I am shocked i didn't think of it, but i'm glad you helped me.
1

Use express.js. Place the socket.io file in public/javascripts folder and add this line to your html

<script src="/javascripts/socket.io.js"></script>

I think this is the best way. When you're writing http://<hostname>:8000/socket.io/socket.io.js
node tries to find a folder named socket.io in your project's public folder. And the file socket.io.js in it.

If you don't want to use express.js you should catch the request and try to load a file if no routes were found for your request (what actually express does) because node doesn't know what to do for requests which don't match any routes in your server.

And I recommend to use the socket.io.min.js file (it's smaller and it's in folder node_modules\socket.io\node_modules\socket.io-client\dist)

5 Comments

Thanks for this, but I dont want to use express for the moment, because I dont know much about it and would rather stay away from other frameworks unless I definitely need them. I have socket.io running on my local machine fine, and am trying to get it working in the same way on my production server. I will look into express if I cant get it working though.
You should add a route for all other requests. And try to send file from server... Believe me, it's ok if you're using express. It's a really cool lib.
I just havent heard of it before, but nothing else is working, so I think you might be right. Just really annoying because I was just about to deploy this, and now I gotta go back and integrate a whole new framework. Oh well :) I have some reading to do.
You do NOT need to have the socket.io.js file on disk at all. A properly configured socket.io will just serve it directly for you with code built in to the NPM module.
@shift66 I will do, always good to learn new stuff, thanks. Having looked into this current problem though, I dont think that will fix it. It seems like node wont serve anything through http at all, so this is my first thing to look at.
1

You have to start an http/https server to access it via http/https. Simply starting an socket.io server won't do. Do the following:

var http = require('http');
var  app = http.createServer(),
io = require('socket.io').listen(app);
app.listen(7000, "0.0.0.0");

Then I can access the file http://localhost:7000/socket.io/socket.io.js

sockets.io uses websocket protocol (ws://). See the wikipedia page.

2 Comments

Thanks, I tried this, but its still hanging. Even just hitting port 8000 on my server in a browser just hangs: richsavage.co.uk:8000
Actually, I just tried a simple "hello world" with the node http server, and thats not working either. I think that must be the problem, node just wont serve anything over http at all. I'll do some reasearch and maybe start a new question later.
1

You need to get at least 3 pieces working together.

  1. Serve some HTML (/index.html will do just fine) so there's a web page. This file should contain the socket.io client <script> tag. For this you need the http server portion of the starter examples. You are missing this and that's why browsing to your server just hangs.

  2. Serve the socket.io client. Socket.io will do this for you automatically when you pass in your http server function to it. You don't need full express as this can be done with just node's http module as per the first example on the socket.io docs.

  3. Some javascript to actually do something with the socket. This can be the content of a <script> tag in index.html or a separate file. If it's a separate file, you need to set up your http server to actually serve it.

7 Comments

Thanks. I have all this in place, as I have it working perfectly on my local machine. The problem is that on my live server, in the browser, socket.io is trying to communicate via http through port 8000, and node just isnt serving anything. On closer inspection it seems that node wont serve anything through http at all!
Firewall? Just with that one line you posted, I get a "Welcome to socket.io" HTML document when I GET /, so if that isn't working on your server and your TCP connections are just hanging, that indicates perhaps a network level firewall is filtering your traffic.
I checked the firewall in my Plesk admin and everything looks ok, but yeah, I hadn't thought of something network level. I'll call my hosting provider and see if they have something in place. Thanks!
If you have ssh access, login to the server directly and run curl localhost:8000. If that works, the node stuff is good. Then check curl <your_real_ip>:8000 to see about a host-based firewall allowing loopback but blocking routable IPs. If both of those work, it's network level.
Actaully, I was testing the wrong port. It DOES work with localhost. And now... Holy Cow... I've worked it out. The port is blocked on the network in my company's office. When I tried through my phone on 3g, everything worked. So many hours wasted... Thanks for helping me pinpoint it though!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.