Where is the "socket.io.js" file located at?

11,470

Solution 1

Your HTML is incorrect. Here's the line you need:

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

It's confusing because there's no obvious socket.io.js file just sitting there on disk. It's served automatically by socket.io within your express web application but the path /socket.io/socket.io.js is totally virtual and does not map directly to directories or files on the filesystem.

Here's the source code where the socket.io server loads the socket.io.js file source code from the socket.io-client npm module, which it will then send to the browser when the URL /socket.io/socket.io.js is requested.

If you want to just grab the file and stick it in your PHP server, it lives here on the official socket.io-client github repo

Solution 2

You can use this one available from a cdn server. Just download and use it! In case you are not using the latest version, then you should visit this one and select your current version.

Share:
11,470
Junior
Author by

Junior

Updated on June 05, 2022

Comments

  • Junior
    Junior almost 2 years

    I am trying to setup a WebSocket to run a simple chat app.

    I have a user using a PHP app that is running on server A. On the other side, I have Server B which is running a WebSocket using node.js and Socket.io.

    I followed the Socket.io tutorial on writing a small chat app. But it seems that I need to include the socket.io.js file in my client's script to start the connection between the user and the Websocket. But, I can't seems to figure out where to get the socket.io.js file from.

    Where can I find socket.io.js at?

    Not sure if my code matter or not in this case but here it is if needed.

    Here is my socket.js file "Websocket" server

    var env = require('./config');
    
    var app = require('express')();
    var server = require('http').Server(app);
    var io = require('socket.io')(server);
    
    server.listen(env.socket.port, env.socket.host, function () {
      var host = server.address().address;
      var port = server.address().port;
    
      console.log('Example app listening at http://%s:%s', host, port);
    });
    
    app.get('/', function (req, res) {
        res.send('Landed!');
    });
    
    io.on('connection', function (socket) {
      socket.emit('news', { hello: 'world' });
    
      socket.on('my other event', function (data) {
        console.log(data);
      });
    
    });
    

    Here is my client code

    <!doctype html>
    <html lang="en-US">
      <head>
        <title>Socket.IO chat</title>
        <style>
          * { margin: 0; padding: 0; box-sizing: border-box; }
          body { font: 13px Helvetica, Arial; }
          form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
          form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
          form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
          #messages { list-style-type: none; margin: 0; padding: 0; }
          #messages li { padding: 5px 10px; }
          #messages li:nth-child(odd) { background: #eee; }
        </style>
    
        <script src="/socket.io.js"></script>
        <script type="text/javascript" src="/js/jquery-2.1.0.min.js"></script>
        <script>
          var socket = io();
          $('form').submit(function(){
            socket.emit('chat message', $('#m').val());
            $('#m').val('');
            return false;
          });
          socket.on('chat message', function(msg){
            $('#messages').append($('<li>').text(msg));
          });
        </script>
    
      </head>
      <body>
        <ul id="messages"></ul>
        <form action="">
          <input id="m" autocomplete="off" /><button>Send</button>
        </form>
      </body>
    </html>
    
    • Junior
      Junior over 8 years
      Not sure why the vote down! I will appreciate an explanation by the voter.
    • Kevin B
      Kevin B over 8 years
      The tutorial shows the script as being at /socket.io/socket.io.js, assuming the page is being rendered from the same server as the socket server. This should quickly translate to http://mysocketserver.com/socket.io/socket.io.js You could also download the socket.io client directly from the site or a cdn.
    • Junior
      Junior over 8 years
      @KevinB thank you for your help. I included the file but I am still getting this error ReferenceError: io is not defined
    • Kevin B
      Kevin B over 8 years
      If you look to your console, you should also see that socket.io.js wasn't found, that's far more important than the js error. The only way to fix the js error would be to correctly reference the file.
    • Junior
      Junior over 8 years
      I was getting that error because of the proxy. I no longer get that error but still unable to loaded it. I get this 404 error now https://sub.domain.com/socket.io/ when I open the socket.io/socket.io.js file in the browser I can see it with no problem
    • arnold
      arnold over 8 years
      I believe this has already been discussed (answered) [here][1]. [1]: stackoverflow.com/questions/17757728/…
    • arnold
      arnold over 8 years
      I believe this has already been answered [here][1]. [1]: stackoverflow.com/questions/17757728/…
  • Kevin B
    Kevin B over 8 years
    Note, that html file isn't on the same server as the websocket server. The websocket server doesn't seem to serve any html.