I made a simple chat program like this. When I tried accessing the same page URL from another computer, there was no response. Is there a way to solve this problem?
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>P2P chat</title>
<style>
#peer-id {
margin-bottom: 10px;
}
#peers {
margin-bottom: 10px;
}
#messages div {
margin-bottom: 5px;
}
</style>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://unpkg.com/[email protected]/dist/peerjs.min.js"></script>
<script>
const peer = new Peer();
peer.on('open', (id) => {
$('#peer-id').text(`Your ID: ${id}`);
});
peer.on('connection', (conn) => {
$('#peers').append(`<div id="${conn.peer}">${conn.peer}</div>`);
conn.on('data', (data) => {
$('#messages').append(`<div>${conn.peer}: ${data}</div>`);
});
});
$('#send').click(() => {
peer.connections.forEach((conn) => {
conn.send($('#message').val());
});
$('#message').val('');
});
</script>
</head>
<body>
<div id="peer-id"></div>
<div id="peers"></div>
<div id="messages"></div>
<input type="text" id="message">
<button id="send">Send</button>
</body>
</html>
I am a programming beginner, so I am having trouble finding a solution. I want to create a chat room that can accommodate three or more people using PeerJS. I am curious if it is possible to create a chat room that can accommodate three or more people using PeerJS.
Your code does nothing but create its own ID, but you also need to find out and specify the ID of the user you want to connect with.
Here is the implementation of a multi-user chat. https://github.com/Rubilmax/peerjs-multichat
Remember that PeerJS has some bugs. For example, it does not close the connection if another user exited the chat by closing a tab or browser