myPeer.on('data') firing twice, how to check if a connection already exists or not #1104
-
Having a little problem here with sending a file with peerJS. It works great. But one thing is bugged, which is obviously my code. After sending a file for the second time, the connection receiving the data is fired twice, or even more depending on how many times you receiving data. Is there a way to check if a connection already exists, or can you destroy a connection after the file has been send. sending a file (send.js)
receiving data (receive.js)
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hey @newmediacrew, I believe the simplest solution to your issue would be to use the To check if a connection already exists, you can use the following code: if (peer.connections['remote-peer-id']) {
console.log('Connection already exists');
} else {
console.log('No existing connection');
} If you want to close the connection after sending, you can do so on the receiving peer with this code: conn.on('data', (data) => {
let blob = new Blob([data.file], {type: 'application/octet-stream'});
saveAs(blob, data.filename);
conn.close();
}); |
Beta Was this translation helpful? Give feedback.
The problem has been solved. I turns out
was inside an event listerner block triggering multiple times. Moving it outside solved the problem.