PeerJS peer not receiving data

2.3k views Asked by At

I intend to use PeerJs to create P2P connections (data and later video). I checked the tutorial and used the following code.

In browserA (Chrome 38):

var local;
var remote;

function peerJsInit() {
    //listening host
    local = new Peer(
    {
        key: 'myPeerJSKey'
    });
    local.on('connection', function(conn) {
        alert('Connection is open');
        conn.on('data', function(data) {
            alert('Data: '+data);
        });
    });
}



function peerSend() {
    remote = new Peer(
    {
        key: 'myPeerJSKey',
        debug: true
    });
    var c = remote.connect('remoteId');
    c.on('open', function() {
        alert('connected');
        c.send(' peer');
    });

}

In browserB (Chrome 38):

var local;
var remote;

function peerJsInit() {
    //listening host
    local = new Peer({
        key: 'myPeerJSKey'
    });
    local.on('connection', function(conn) {
        alert('Connection is open');
        conn.on('data', function(data) {
            alert('Data: '+data);
        });
    });
}



function peerSend() {
    remote = new Peer({
        key: 'myPeerJSKey',
        debug: true
    });
    var c = remote.connect('remoteId');
    c.on('open', function() {
        alert('connected');
        c.send(' peer');
    });

}

The Connection is open message showed, but the Data and connected messages never.

Can you tell me what I should change?

1

There are 1 answers

2
Nestor On BEST ANSWER

It seems that I forgot to add another callback. It is sooo good that JS compiles without a problem, even when callbacks are missing... piece of g*rbage...

So instead of this:

 local.on('connection', function(conn) {
        alert('Connection is open');
        conn.on('data', function(data) {
            alert('Data: '+data);
        });
    });

I have to use this:

 local.on('connection', function(conn) {
        alert('Connection is open');
        conn.on('open',function(){
        conn.on('data', function(data) {
            alert('Data: '+data);
        });
       });
    });