Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

211
Visualizações
How to set up a basic Peerjs connection?

I am quite new to Javascript so please bear in mind I may be quite slow to understand things. I have spent several hours trying to get a very simple peerjs connection where there are two users who run a html file in the browser, connect and then send hello to eachother. I've tried everything I can think of and have looked at all the documentation but can't seem to even get a connection to work. This is what I have so far (largely taken from this page https://peerjs.com/docs.html):

For User1:

<html>
    <body>
        <script src="https://unpkg.com/peerjs@1.3.1/dist/peerjs.min.js"></script>
        <script src="test1.js"></script>
    </body>
</html>

__test1.js__
var peer = new Peer("User1");
peer.on('open', function(id) {
  console.log('My peer ID is: ' + id);
});
var conn = peer.connect('User2');

For User2:

<html>
    <body>
        <script src="https://unpkg.com/peerjs@1.3.1/dist/peerjs.min.js"></script>
        <script src="test2.js"></script>
    </body>
</html>

__test2.js__
var peer = new Peer("User2");
peer.on('open', function(id) {
  console.log('My peer ID is: ' + id);
});
peer.on('connection', function(conn) { console.log("Connected") });

When I run the second HTML file followed by the first, I correctly get their ID's outputted but the connection never happens. What am I doing wrong? Thank you.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to connect to a peer after connection to the server is established.

This means you need to invoke connect after the open callback:

var peer = new Peer("User1");
peer.on('open', function(id) {
  console.log('My peer ID is: ' + id);
  var conn = peer.connect('User2');
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda