Simple-peer: Peer.send is a null property ?

Created on 18 Sep 2017  路  11Comments  路  Source: feross/simple-peer

I justed tried out simple peer today and tried sending a message

document.getElementById('connect').addEventListener('click', () => {
    const recip = JSON.parse(document.getElementById('recip').value)
    Peer.signal(recip)
})
Peer.send(message)

I keep getting this error
bundle.js:15006 Uncaught TypeError: Cannot read property 'send' of null

All 11 comments

I checked the property and the Peer object instance is not null and the __proto also has the connect method on it, could this be bad prototype chaining the class or inheritance problem @feross

@andreGarvin how did you solve this?

I'm also wondering how you solved this...

You cannot use peer.send() before the connect event.

Use peer.write() to take advantage of the buffer.

That's not my issue @RationalCoding. I'm trying to send to a peer that I've already sent to before, but for some reason in a specific case it is failing with TypeError: Cannot read property 'send' of null. The object instance exists and seems normal when I log it. Do you have any ideas?

For example, this code is failing:

if (p) {
    p.send(JSON.stringify(message));
}

Can you try logging peer.connected and peer.destroyed before that call to send?

In my case it was making the mistake of calling/setting the same peer.on(..) handlers more than once (took me a lot of re-reading my code), or having mistakenly replaced the peer2 instance variable with a new peer2 instance variable (in which case, this new instance isn't the one connected yet so it won't have a .channel property, which could lead to error when trying to call .send).

In my bad code, this mistake in replacing the instance variable happened during the peer1 ice trickles signals.

@owenauch if your peer1 has ._channel property and peer2 does not, this might also be the case.

What is the difference between write and send?

write is a part of streaming interface, can be buffered, while send sends data immediately and directly through data channel: https://github.com/feross/simple-peer#memory-usage

I am soooooo sorry I did not see earlier @xemasiv

Was this page helpful?
0 / 5 - 0 ratings