2

I am trying to develop a live chat application using laravel. I face a problem. When I run "node index.js" , 'A connection has made' message has shown continuously in command prompt.

my index.js file is:

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 has made');
    // socket.on('chat.message', function(message){
    //  io.emit('chat.message', message);
    // });
});

My index.html page is:

<!DOCTYPE html>
<html>
    <head>
        <title>Live Chat</title>
    </head>
    <body>
        <div class="container" id="chat">
            <h1> Chat System </h1>
        </div>
    <script type="text/javascript">
    var socket = io();
    </script>
    </body>
</html>

How can I solve it?

rakib
  • 197
  • 8
  • Possible duplicate of [Socket.IO infinite loop on connection](https://stackoverflow.com/questions/44913564/socket-io-infinite-loop-on-connection) – thestruggleisreal Oct 14 '18 at 18:40

3 Answers3

11

The usual reason that your client is continually trying to connect over and over again is because you have a mismatched client and server version of socket.io making them incompatible. You don't show how you load the socket.io Javascript in your web page, but if you do it like this:

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

Then, you will always get the version that exactly matches your server from your server automatically (this is a route that the socket.io server automatically adds to your Express server).

If you are loading socket.io from a CDN, then you have to either switch to the above to load it from your own server or manually specify the exact same version from the CDN as you are running on the server.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thank you so much! This answer is gold. Saved me hours and hours of trying to find out why socket.io client goes in a loop. – Vlad K. Nov 11 '20 at 12:18
0

If you are using it on React/NextJs just declare these as global

import socketClient from "socket.io-client";
const SERVER = "http://127.0.0.1:8000";
const socket = socketClient(SERVER);
export default chatApplication=()=>{
    //main component 
}
azibom
  • 1,769
  • 1
  • 7
  • 21
-2

Change port number from 3000 to 7000 for example.

Denis
  • 1
  • 1
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/low-quality-posts/26106058) – Chris May 12 '20 at 19:58
  • 2
    @Chris: It _might_ provide an answer. For example, if Denis believes there's some type of port conflict on 3000 then changing the port could be a solution. If so, however, this answer would definitely benefit from additional explanation of _why_ the OP should change their port—and especially given that there's an accepted answer with a number of votes proposing a _different_ solution. – Jeremy Caney May 13 '20 at 00:11
  • When possible, please make an effort to provide additional explanation instead of just code. Such answers tend to be more useful as they help members of the community and especially new developers better understand the reasoning of the solution, and can help prevent the need to address follow-up questions. – Rajan May 13 '20 at 04:33