0

Alright, so I had my socket.io server listening on a different port, but in order to get it to work with https, I needed to have it listen without passing in a port (default). (It works fine on a different port loaded with http, but I need it to work on https)

My project was working fine, client could connect and send data fine. However, I moved the site over to my main domain, which has an SSL certificate. The site loads everything via https, so it couldn't load the http version of socket.io.js

However, now that I switched it to just var client = require("socket.io").listen().sockets; instead of listening on a different/specific port , it's still not working. Instead of giving me a connection error, it's not including the file at all.

My fear is that I'd end up needing to remake my whole site to host my files via node.js and I'd rather not have to do that.

I'm not using any other module than mysql-node and socket.io, and I'd prefer to keep it that way if possible. I am new to node.js, so I'm sorry if there's an obvious answer that I'm unaware of.

I looked around, however, and can't seem to find the answer anywhere. Or, at least a clear answer.

Would I be better off using websockets instead of socket.io? If so, how would I go about doing this? I'd be more willing to remake my node application instead of remaking my site, honestly.

I am including the socket.io.js file in the client-side like so:

<script src="https://mysite/socket-io/socket.io.js"></script>

but of course, 404 since it's not an actual file that's on my apache server. There's no folder/directory named socket-io in my public_html directory, so that makes sense to me.

But, how can I get this to work? Would I need to host my files via node.js or would I be better off using HTML5 websockets? A fairly large demographic of my site's users use mobile devices, so I'd have to be sure it works on mobile as well.

1
  • Why you are using two separate web servers (Apache and node.js) to make your site? How do you have that set up architecturally? Are you trying to serve the socket.io.js file with the Apache server or with the node.js server? Commented Aug 9, 2015 at 7:42

1 Answer 1

1

If you're going to use apache to host the socket.io.js file, then you need to put that file on your Apache server at a path that it can be served from by Apache, just like any other web file that you want the Apache server to serve. Or, you can just serve socket.io.js from a public CDN also and use the public CDN URL. It's just a JS file. You can put it anywhere or use any URL that reaches a place where the file will be served from. There are some advantages to letting node.js and socket.io serve it for you because it guarantees that client and server socket.io versions are always in sync, but you don't have to do it that way.

If you are using node.js (which it sounds like you are at least in some capacity), then the socket.io built into node.js will serve the file automatically if you are using node.js to serve your web page too and you've configured socket.io to listen on the same port as your node.js web server. In that case, your webpage and socket.io will use the same port and both will run through the node.js server.

You haven't really explained why you're using both node.js and Apache, how that architecture works and why you're serving some of your site with Apache rather than just using node.js for the whole site as that is certainly the cleaner option with socket.io.

You can use plain webSockets if you want instead of socket.io, but then you will likely have to build some of the socket.io functionality on top of the webSockets (auto-reconnect, message passing, etc...) and using plain webSockets won't really simplify any of the Apache/node.js questions you have. It's trivial to serve up the socket.io.js file to the client using either Apache or node.js and once the client has the file, it is actually more work to use plain webSockets than to use socket.io because of the extra features that socket.io has already built.

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

6 Comments

I'm using apache to host my files simply because that's what I've been using since the beginning. I probably should've explained more clear; I am using node.js/socket.io to serve the socket.io.js file, but I'm using apache to serve the site (and subdomains) as a whole. I figured I'd be able to add the socket.io project in with the site, and include the js file (which works for http, but not for https). I'm not sure how I'd go about hosting the site itself with node.js, and would https work with that? Would PHP/MySQL work as well? Is node stable enough to keep the site up 24/7? Thanks btw :)
@Axiom - If you're already using PHP with Apache, why not just get a socket.io implementation that works with Apache/PHP rather than introduce a whole additional web server technology into your stack just for webSockets?
Oh! I wasn't aware PHP had ability to do so. Thank you. I'll look into it
@Axiom - if your Apache server is just serving static files, it takes but a few lines of code to have node.js take over the serving of the static files and remove Apache from the equation. socket.io via node.js and https will work a lot cleaner if you are serving the web page from the same server and port that you are connecting the socket.io connection to.
How would I go about hosting my files via node instead of apache? I know node.js has the "http" module but how to with https? Also; Would it still support .htaccess files? Or would there at least be a different way for me to rewrite urls?
|

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.