6

I have followed Jeffrey way's tutorial.
Tutorial:
https://laracasts.com/series/real-time-laravel-with-socket-io/episodes/1

this is my index.js

var app = require('express')(); 
var server = require('http').Server(app); 
var io = require('socket.io')(server);

server.listen(3000);

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

io.on('connection', function(socket){   
   console.log('A connection is made'); 
});

Some people say its the version and year. The code above works on 2014 I think.

This is my html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"></meta>
    <title>Chat Lesson</title>
</head>
<body>
    <h1>Hello world!</h1>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js"></script>
    <script type="text/javascript">
        var socket = io();
    </script>
</body>
</html>

I have done a lot of research. Some people say the sequence is important but I have found so many sequence.

My error:

GET http://chat-lesson.local:8888/socket.io/?EIO=3&transport=polling&t=1515636281477-74 404 (Not Found)

My question:
Why is it 404 not found? I have followed Jeffrey way's tutorial in every step and the tutorial was only short like 5-8mins.

  • Mamp Pro 4
  • NPM 3.10.10

I tried using server.listen(8888);

events.js:160
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE :::8888
    at Object.exports._errnoException (util.js:1022:11)
    at exports._exceptionWithHostPort (util.js:1045:20)
    at Server._listen2 (net.js:1259:14)
    at listen (net.js:1295:10)
    at Server.listen (net.js:1391:5)
    at Object.<anonymous> (/Users/kendantinio/Documents/Freelancer/blvnp/chat-lesson/index.js:5:8)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
KD.S.T.
  • 573
  • 1
  • 5
  • 27
  • You say your PHP version, but don't mention PHP elsewhere. Was that a mistake or something that should be taken into consideration? – Piper McCorkle Jan 11 '18 at 02:09
  • @ZebMcCorkle sorry about that, I have removed and updated PHP – KD.S.T. Jan 11 '18 at 05:16
  • So you were clearly here 3 hours ago, but said nothing about my answer below. Does it show you what's wrong or not? Questions? – jfriend00 Jan 11 '18 at 08:31
  • FYI, your question says you followed a tutorial at https://cdnjs.cloudflare.com/ajax/libs/socket.io/2.0.4/socket.io.js. That's NOT a tutorial. That's a socket.io.js file. – jfriend00 Jan 11 '18 at 14:25
  • 1
    @jfriend00 sorry about that, I updated my description its now the correct tutorial link, thanks! – KD.S.T. Jan 11 '18 at 19:52

4 Answers4

7

I'm adding this because this comes up first on Google and it took me 2 days to find a solution.

I'm using Node.js and React, with socket.io-client

I was able to make it work by passing a url and a configuration object to the io namespace.

import io from 'socket.io-client'
const socket = io('http://localhost:3000', {
    reconnectionDelay: 1000,
    reconnection: true,
    reconnectionAttemps: 10,
    transports: ['websocket'],
    agent: false,
    upgrade: false,
    rejectUnauthorized: false
});
JJJ
  • 3,314
  • 4
  • 29
  • 43
3

Your server is listening on port 3000, but the socket.io URL you show is port 8888. If you do have some other server running on port 8888 (perhaps your PHP server), it's not this one that's running socket.io.

To understand exactly what fix is needed, this raises a question about what's the URL of the page you show the HTML for? What does the browser show in the URL bar when you display that page? Your node.js code implies you're intending to load that page from the port 3000 node.js server, but the client code you show would not be loading it from port 8888 if that's the case.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • I updated my description. I did `server.list(8888)` but showing error – KD.S.T. Jan 11 '18 at 20:21
  • @Kenn - It would be `server.listen(8888)`. And, you did not answer my question about what the URL bar in the browser shows when that page is displayed? – jfriend00 Jan 11 '18 at 22:25
3

Try doing this code.

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

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

users = [];
io.on('connection', function(socket) {
    console.log('A user connected');
    socket.on('setUsername', function(data) {
        console.log(data);

        if(users.indexOf(data) > -1) {
            socket.emit('userExists', data + ' username is taken! Try some other username.');
        } else {
            users.push(data);
            socket.emit('userSet', {username: data});
        }
    });

    socket.on('msg', function(data) {
        //Send message to everyone
        io.sockets.emit('newmsg', data);
    })
});

http.listen(3000, function() {
   console.log('listening on localhost:3000');
});

Let me know if it works.

Marlon Buendia
  • 342
  • 1
  • 6
  • 19
1

I am almost sure Marlon´s code worked because http.listen is at the end of index.js

Yuzam
  • 55
  • 8
  • In all examples i have found `listen` is always the last instruction on the server, i don´t remember where, but i think i saw "Make sure to call listen the last" somewhere. I supose this is because you are telling the server to start listening for requests before you have defined how these requests are. But i am not sure, learning Socket.io and Express since days. – Yuzam Sep 05 '18 at 10:39