0

I have my node.js server running and communicating on my local machine (windows). But when i copy my project and try to run it on my online server (Linux), socket.io doesn't communicate from server to client (html page). I am getting no errors and socket.io is installed on server. The directory is /var/www/lynx.html

How do I direct the server to communicate with the client, and the client to connect with the server. I have tried to change http:\\localhost to all kinds of things like the actual ip address and local 127.0.0.1

Here are the important parts of my code.

Client

<script type="text/javascript">
    var socket = io.connect('http://localhost');
    socket.on('message', function(message){        
        var obj = jQuery.parseJSON(message);
        var htmlStr =''; 

Server

    var dgram = require("dgram");
    var udpResultsServer = dgram.createSocket("udp4");
    var udpTimeServer = dgram.createSocket("udp4");
    var http = require('http');
    var io = require('socket.io');

    var htmlPage;
    var jsonResults;
    var tempResults = '';
    var finalResults = '';
    var time = '0.0';
    var r = new Array();

    /*  
    ************************************
    ** Get Ip Addresses               **
    ************************************
    */
    function getIP(){
        var os = require( 'os' );
        var networkConfig = os.networkInterfaces( );    
        var ipList ='';    
        for (var name in networkConfig) {
            networks = networkConfig[name];    
            for (var name in networks) {        
                details = networks[name];       
                if (details['family'] == "IPv4"){    
                    ipList = ipList + details['address'] + " ";
                }
            }
        }
        return ipList;
    }


    /*  
    ************************************
    ** Web server and sockets.io       **
    ************************************
    */
    var htmlPage;

    // read html file to serve
    fs.readFile('lynx.html', function(error, data){
        if (error){
            throw error;
        }
        else{
            htmlPage = data;
            console.log('Server wrote to lynx.html file');
        }
    });

    // start web server 
    var htmlServer = http.createServer(function(request, response) {
        response.writeHeader(200, {"Content-Type": "text/html"});
        console.log('Client Connected ....');
        response.end(htmlPage);

    });
    htmlServer.listen(8000,'127.0.0.1');
    console.log("HTML server started on: " + getIP());

    // start up socket.io for transmitting data to the web page
    var serv_io = io.listen(htmlServer, { log: false });
    serv_io.sockets.on('connection', function(socket){
        //send data to client
        setInterval(function(){
            jsonResults = JSON.stringify(r);        // convert final array into a json string
            socket.send(jsonResults);               // sent json results via socket
            console.log('send <--> receive');
        }, 1000);
    });

These are the errors when I installed Socket.io locally in the drive I am working on, would this cause the errors.

I also installed it globally -g and there were not the errors.

 npm WARN package.json fs@0.0.2 fs is also the name of a node core module.
 npm WARN package.json fs@0.0.2 No description
 npm WARN package.json fs@0.0.2 No repository field.
 npm WARN package.json net@1.0.2 net is also the name of a node core module.
 npm WARN package.json net@1.0.2 'repositories' (plural) Not supported. Please pick one as the        'repository' field
 npm WARN package.json has-binary-data@0.1.5 No repository field.
 socket.io@1.2.1 node_modules/socket.io
 âââ debug@0.7.4
 âââ has-binary-data@0.1.3 (isarray@0.0.1)
rage12345
  • 5
  • 3

1 Answers1

1

If your server is on the same host and port that your webpage is loaded from, then you should be able to just do this in the client:

var socket = io();

and it will pick up the same host/port as the web page was loaded from and make a socket.io connection to that.


If your socket.io server is not on the same host/port as the webpage, then you may have some cross origin issues that will need to be solved.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I have already tried that, and it doesn't resolve the problem. What can I search ? – rage12345 Dec 11 '14 at 22:56
  • @rage12345 - You can look in your browser error console and in the network tab of the debugger to see what is happening from the client side. You should be able to see the initial http request leave the client to start the socket.io connection and then you can see what kind of response you get. A normal socket.io connection starts with a couple http requests and then the two sides agree to switch protocols to the webSocket protocol and then the Chrome debugger shows further communication in the webSocket sub-tab. – jfriend00 Dec 11 '14 at 22:58
  • @rage12345 - see [this answer](http://stackoverflow.com/questions/27231590/socketio-tries-to-connect-using-same-port-as-the-browser-used-to-get-web-page/27245753#27245753) for what you should normally see in the network tab of the debugger for establishing a socket.io connection. – jfriend00 Dec 11 '14 at 23:03
  • I am getting a similar error to what you showed me, but I can't figure out where I am going wrong. I admit I am new to this node.js and socket.io. I had to change the to following to get the script to show. `` – rage12345 Dec 12 '14 at 01:46
  • I installed Socket.io both globally and locally in the drive I am working /var/www/ however when I installed it locally on /var/www It through some warnings I will show above. does that matter? it is pulling from the users/lib/... or from the var/www/ – rage12345 Dec 12 '14 at 01:55
  • @rage12345 - Assuming you are using socket.io v1.0 or higher, then your client should just be `var socket = io();` and it will take care of the URL for you. What errors so you get when you just use that? If a GET on `yourserver/socket.io` is giving you a 404, then your server code is not set up correctly because that is the initial URL for a socket.io connection and the server is supposed to have a route for that URL if the server is set up correctly. I use `express` with my node server so I don't quite know how to set up socket.io without express other than what the socket.io doc says. – jfriend00 Dec 12 '14 at 05:27
  • I am getting the 404 error, and I don't know what is wrong with my server. I know the socket.io is installed and so is node, and they are on the latest versions. It does not know where the socket.io.js file is. What do I need to do to make sure my server is setup to use sockets and live web apps – rage12345 Dec 18 '14 at 01:46
  • If you're getting 404 when socket.io tries to connect the webSocket, then the socket.io server is not handling the socket.io connection route properly so something is wrong with the socket.io server-side installation. If you're getting 404 when trying to load the client side `socket.io.js` file, then you probably need to fix the client side path to that file. – jfriend00 Dec 18 '14 at 02:53
  • I am unsure of what was the problem, but after reinstalling node, and socket.io it started working. – rage12345 Dec 30 '14 at 04:27