1

It this possible, or do you have to force another socket.io connection to the client when he enters another HTML file in the same web area?

1

2 Answers 2

1

i think 1 connection will do the job. Look at this code:

    var io = require('socket.io')(server);
    io.sockets.on('connection', function(socket) {
        console.log("user connected");
        socket.on('from_client', function(data) {
            io.sockets.emit('to_client', data); 
        });
    });

Above 'user connected' is printed when a new when a new browser tab opens localhost:8000 and thus, you can use the same socket.io for every open browser window(emit sends the msg to all the clients). Thus, as for what you asked, the answer is no, changing the html of an open/existing client doesnt require you to create a new socket/

Sign up to request clarification or add additional context in comments.

1 Comment

But when I try to access the socket variable I created in another HTML file it says that it is undefined.
0

We cant create a connection with same socket id for all html page.Because when we try to reload or redirecting to new page socket connection get disconnected.It will create a new connection with new socket id.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.