Hi, I'm testing the lib, reading the documentation, which seems to be out of date, I understand the lib works in a server/client way like TCP, the server waits for connections, the client initiates the connection. Is that correct?
I tried to create a client and server, which send and receive data from each other when they connect.
The server receives the message from the client, but the client is not receiving the message from the server.
server
-------------
<html>
<head>
<meta charset="UTF-8">
<script src="https://unpkg.com/pee...@1.0.0/dist/peerjs.min.js"></script>
<script>
// server
var peer = new Peer('3b32afd4-b3c7-4abb-a041-6a6ce01ceee6');
// receive connection
peer.on('connection', function(conn) {
console.log(conn);
console.log('peer id', conn.peer);
conn.on('data', function(data){
// Will print 'hi!'
console.log(data);
});
conn.send('hi from server');
conn.on('error', function(err){
console.log(err);
});
});
</script>
</head>
<body>
</body>
</html>
client
----------------
<html>
<head>
<meta charset="UTF-8">
<script src="https://unpkg.com/pee...@1.0.0/dist/peerjs.min.js"></script>
<script>
// local
var peer = new Peer();
// connect to server
var conn = peer.connect('3b32afd4-b3c7-4abb-a041-6a6ce01ceee6');
// start connection
conn.on('open', function() {
console.log('client conn', conn);
console.log('peer id', conn.peer);
// here you have conn.id
conn.send('hi from client!');
conn.on('data', function(data){
console.log(data);
});
conn.on('error', function(err){
console.log(err);
});
});
</script>
</head>
<body>
</body>
</html>
What is going on? Am I missing something?
Other questions:
1. What mechanisms are used to distribute the id of the server to the clients? Because the client needs that ID but the doc doesn't include a complete example considering that part.
2. When I have two users that need to connect, what is the best approach to connect them? I mean: both are using the same app and have the same hierarchic level, but in the way this library works, one should be a server and the other a client, so how to decide which one is server and which one is client?
3. Can I use this library for a multi-user chat? I need multiple users to talk at the same time on a widget that will appear on a web app, they will be peers on the same connection for that.
Thanks a lot!