peers / peerjs

Simple peer-to-peer with WebRTC.
https://peerjs.com
MIT License
12.38k stars 1.43k forks source link

unavailable-id after internet connection lost #224

Closed alioguzhan closed 5 years ago

alioguzhan commented 10 years ago

Hey, this is how i reproduce this error:

if i close and re-open internet connection, it throws this error.

PeerJS:  WARNING You received a malformed message from undefined of type HTTP-ERROR peer.js:1117
PeerJS:  Socket open peer.js:1117
PeerJS:  ERROR Aborting. Error: ID `1` is taken peer.js:1117

and i dont use random ids generated from peerserver. a client's peer id is always same and unique (i am getting it from user's primary key value from database)

It is ok while i am switchinh between my web sites pages. (i put peer initialization code in my base.js which loads in every page) ,

swtiching between pages is a kind of disconnection from server right? but why does this happen only when internet connection lost?

i handle this error under peer.on('error'), although i write peer.destroy() here, i doesn't work.

maasud commented 10 years ago

same problem here. Only happens when internet connection gets reset and no luck with peer.destroy()

alioguzhan commented 10 years ago

i think (and hope) @michelle can help us on this.

for now, i switched to redis-based peer-id system. i am keeping all users' peer ids in redis, and every time i got this error, sends a request via ajax to server to generate new id for this user. and on the success (if id generated successfully) i am reloading the page. and user gets just generated peer-id. @maasud

michelle commented 10 years ago

Yeah it seems like the websockets don't emit 'close' properly/consistently on peerserver, but I'm at a loss for why that's the case.

ncri commented 9 years ago

Any progress/workarounds here? Just ran into the same issue...

trakers33 commented 9 years ago

Same for me

arjehouw01 commented 9 years ago

I ran into the same problem. Is peer.reconnect an option?

trakers33 commented 9 years ago

Just destroy and connect. Reconnect gonna fail too

shashvattrip commented 9 years ago

Still facing this issue. @peers please solve this issue.

shashvattrip commented 9 years ago

@michelle : I have a temporary solution. When the peer server emits an error saying that peer id is taken, can we check if there are any active connections on the same id. If no, then the peer server can flush destroy that peer id from the server. Would this work?

ncri commented 9 years ago

Any updates here? peerjs doesn't seem to be very maintained. I might try to modify the peer server and try out your idea @shashvattrip.

shashvattrip commented 9 years ago

Hey @ncri , yes please do. I too will try it out soon once i get a little time from my current project.

ncri commented 9 years ago

Okay, well, also a bit short on time, but hope to look into it soon. So far I got a working solution, although not a very satisfying one: after a few retries the id becomes available again...

shashvattrip commented 9 years ago

Yes, same here. I have tried changing the peer id on id not available. But I don't want to hack it, there needs to be a straight forwards working mechanism to handle this scenario

ncri commented 9 years ago

I was able to avoid the error by passing in the same token when recreating the peer object after the connection is back. Not sure if this is ideal, but it works well so far.

interfaith commented 9 years ago

If peerjs-server can be modified to remove that user, that will do. Is there a working fix now ?

bialabs commented 8 years ago

same problem here. tried with peer.destroy() but nothing. Anyone can help me?

domschiener commented 8 years ago

Anyone found a good solution yet? I'm using hardcoded ID's to make it easy for people to connect to each other and store local data (about the other peer), so changing ID's is not the nicest solution for me.

forrana commented 7 years ago

https://github.com/peers/peerjs/blob/master/dist/peer.js#L733 Can someone explain the meaning of this string?

I made an investigation in server code and found that issue in that string, probably. https://github.com/peers/peerjs-server/blob/master/lib/server.js#L255

So how it works now - client every time when reconnect, generate a new token, server check it and generate an error message in response. How I solve it:

const token = util.randomToken();
...
  options = util.extend({
    debug: 1, // 1: Errors, 2: Warnings, 3: All logs
    host: util.CLOUD_HOST,
    port: util.CLOUD_PORT,
    key: 'peerjs',
    path: '/',
    token: token,
    config: util.defaultConfig
  }, options);

What possibly wrong could be with my solution? Because I really don't understand why we can't keep one token for a page.

rahilka commented 7 years ago

Any updates on this issue? peer.destroy() is not helping...

LucaColombi commented 6 years ago

Same problem after 4 years, I suppose there is a maintainance problem :|

Is there a id timeout or a way to stimulate the real taken status of an ID?

kidandcat commented 6 years ago

Hi, we are maintaining it again, but I cannot go through all old issues checking if they have been fixed or not, browsers have changed a lot.

Let me give it a look to see if I can find a solution. Of course any contribution is welcome.

kidandcat commented 6 years ago

As mentioned before, the close event don't work for lost connections very well. My idea is to give a unique token (password) when you connect using an ID, and if another peer connects using that ID and that token, then I will suppose it is you, and will accept your connection.

What do you think about this approach?

LucaColombi commented 6 years ago

Hello,

technically, it would work, but I am concerned about security vulnerabilities, in this way, I can in any moment impersonate another ID and get messages that were directed to him.

If there are no other ways, your solution is better than nothing, so static token ids can works with peerJS (otherwise we would need to excange dinamic ids.. almost like another signaling server)

I would have some idea to improve security, I don't know the under code so I am telling things that could be easy or not to do, you evaluate the feasibility:

Is there possibile to check a lost connection with a timeout and autodisconnect the lost connections? Otherwise on demand (this would be lesser resource consuming), when the same id reconnect, is it possible to check if the previous connection is alive (like a ping)? if the previous answer in X seconds the new connection is refused, otherwise, the previous is closed and the new wins, is it possible?

Thank you for your time

kidandcat commented 6 years ago

Well, you will not be able to impersonate because you need the initial Token (password) PeerServer sent to you the first time you connected.

About your proposal, the thing is, usually you need to give few seconds to the ping to come back. And what you propose, it will work, but if you lose connection and reconnect, you may need to wait about 30 secs. That's a lot of time.

LucaColombi commented 6 years ago

o ok, I didnt know the protocol, so seem perfect

LucaColombi commented 6 years ago

Hello,

it seems to me that the token is already shoult be used to accept the user connection in the server, is it possible? Can you tell me if I am wrong?

in this case the problem would be that the token changes, it could by simply reloading the page if not put in a cookie or similar, as a fix, could the token be forced in the client side?

app._configureWS = function(socket, key, id, token) {
  var self = this;
  var client = this._clients[key][id];

  if (token === client.token) {
    // res 'close' event will delete client.res for us
    client.socket = socket;
    // Client already exists
    if (client.res) {
      client.res.end();
    }
  } else {
    // ID-taken, invalid token
    socket.send(
      JSON.stringify({ type: "ID-TAKEN", payload: { msg: "ID is taken" } })
    );
    socket.close();
    return;
  }
RamyaAshika commented 5 years ago

What's the update for this? @kidandcat I'm also facing the same problem when I'm trying to reconnect with the same peer. Its throwing error and its getting destroyed.

afrokick commented 5 years ago

We added heartbeat message from client to server. So, i think we can check it and close connection if it missed

afrokick commented 5 years ago

fixed in 1.0.4+

Fayozjon commented 4 years ago

Same error NOT FIXED yet

jose920405 commented 4 years ago

Still happening and is sporadic

naveedaziz commented 4 years ago

Its killing,,,,, what to do for the error

naveedaziz commented 4 years ago

Does PeerJS is recommended for some major projects. As these kind of errors killing the project.