OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

PeerJS - connection.on('open') not executing

  • Thread starter Thread starter user13585926
  • Start date Start date
U

user13585926

Guest

  1. When peer 1 connects to peer 2
    1. the highlighted code in the picture should fire
    2. peer 2 should send peer 1 "hello!"
    3. peer 1 should have "hello!" printed in its console

  2. Peer 1 connects to peer 2


  3. Problem: peer 1 does not have "hello!" printed in its console

enter image description here

Code:
// make a new peer
const peer = new Peer('', {
  host: '/',
  port: '3001'
});


// "connection" event fires when someone tries to connect to us
peer.on('connection', (conn) => {  
  console.log('someone connected');
  
  // "data" event fires when someone sends us a message
  conn.on('data', (data) => {
    console.log(data);
  });
  
  // ===========================================================================
  
  // Problem: Both Attempt 1 and Attempt 2 fail to run
  
  // ATTEMPT 1: "open" event fires when the connection is opened
  conn.on('open', () => {
    conn.send('hello!');
  });
  
  // ATTEMPT 2:
  conn.send('hello!');
  // ===========================================================================
});


// connect to a peer
const conn = peer.connect('another-peers-id');


// after connecting to peer, send "hi" to them
conn.on('open', () => {
  conn.send('hi!');
});

<ol>
<li><p>When peer 1 connects to peer 2</p>
<ol>
<li>the highlighted code in the picture should fire</li>
<li>peer 2 should send peer 1 "hello!"</li>
<li>peer 1 should have "hello!" printed in its console</li>
</ol>
</li>
<li><p>Peer 1 connects to peer 2</p>
</li>
<li><p><strong>Problem:</strong> <strong>peer 1 does not have "hello!" printed in its console</strong></p>
</li>
</ol>
<p><a href="https://i.sstatic.net/B5r7E.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/B5r7E.png" alt="enter image description here" /></a></p>
<pre><code>// make a new peer
const peer = new Peer('', {
host: '/',
port: '3001'
});


// "connection" event fires when someone tries to connect to us
peer.on('connection', (conn) => {
console.log('someone connected');

// "data" event fires when someone sends us a message
conn.on('data', (data) => {
console.log(data);
});

// ===========================================================================

// Problem: Both Attempt 1 and Attempt 2 fail to run

// ATTEMPT 1: "open" event fires when the connection is opened
conn.on('open', () => {
conn.send('hello!');
});

// ATTEMPT 2:
conn.send('hello!');
// ===========================================================================
});


// connect to a peer
const conn = peer.connect('another-peers-id');


// after connecting to peer, send "hi" to them
conn.on('open', () => {
conn.send('hi!');
});
</code></pre>
 

Latest posts

Top