0

I wasn't sure if this is appropriate on the Ubuntu exchange or here but it is mostly code related i believe.

So I have created a neat working little web-chat application using socket.io which I've been developing on my Win10 pc using Git-bash and running it on my localhost with node.js and everything has been working just fine.

Thus I have come to a point where I would like to try out my app on my web-server.

Lets get more detailed what my Problems are:

  1. I am unsure of how my server.js file should listen or open up the ports so to speak. I have tried looking in the sockets.io chat example and tried their method yet the '../..' module leaves me confused.
  2. Following the express.js site tutorial I actually get my server to respond listening on port 3000 yet my website returns the no socket.io/socket.io.js found.
  3. this and this just lead to another localhost tutorial

In Short I have come to the point where when i do node server.js It seems to start listening here's the code to that part:

  var express = require('express');
    var http = require('http');
    var app = express();
    var server = http.createServer(app);
    var io = require('socket.io').listen(server);
    var port = process.env.PORT || 3000;

    app.get ('/', function(req,res) {
     res.sendFile(__dirname + '/index.html');
    });

    app.use("/static", express.static('./static/'));
    server.listen(3000);

But now the problem is is that my html file cannot seem to find the socket.io/socket.io.js file, even though i have installed sockets.io, i consulted to this stack question which should fix it, but it didnt thus leads me to believe theres a thicker more server side issue?

Versions i am using:
  express: **4.16.4**;
  node   : **10.15.0**;
  npm    : **6.4.1**;
  socket : **2.2.0**;

EDIT: Added my html code snips

html

  <script src="/socket.io/socket.io.js"></script>
        <script src="https://code.jquery.com/jquery-1.11.1.js"></script>
        <script src="static/index.js"></script>
11
  • 1
    Where did you get your var io = require('socket.io').listen(server); line from? In my opinion, it should be var io = require('socket.io')(server);. Commented Jan 16, 2019 at 22:55
  • 1
    I always get confused about socket.io implementation to be honest. Right now, I have a running one working where I make Express listen on a specific port (say, server.listen(1111)) and socket.io on another one (io.listen(3000)). If this can be used of any use to you ... Commented Jan 16, 2019 at 23:08
  • 1
    I have, like you, var server = http.createServer(app);, var io = require('socket.io')(server); and server.listen(port), I simply added io.listen(3000). Both Express and socket.io listen to their own port and everything runs smoothly. Could you add your client code to see your <script src> line? Commented Jan 16, 2019 at 23:24
  • 1
    I put your code without changing a single character into a file and put it in a folder on my Raspberry running Lubuntu. I run the file, then I go to http://192.168.220.11:3000/socket.io/socket.io.js on my PC and I see the script just fine. I did notice a small error though: going to http://192.168.220.11:3000 results in an error because express is trying to serve ...pathindex.html instead of ...path/index.html; you need to add a slash /index.html to your / route. Commented Jan 16, 2019 at 23:39
  • 1
    i though have a domain thus i cant change it like that not sure what you mean, you can just go to http://example.com:3000 Don't you have to do that anyway to see the index file? The raspberry also has a hostname; everything works just as well when I go to raspberry.router.box:3000 Commented Jan 16, 2019 at 23:53

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.