{"action":"created","issue":{"url":"https://api.github.com/repos/ipfs/js-ipfs/issues/1753","repository_url":"https://api.github.com/repos/ipfs/js-ipfs","labels_url":"https://api.github.com/repos/ipfs/js-ipfs/issues/1753/labels{/name}","comments_url":"https://api.github.com/repos/ipfs/js-ipfs/issues/1753/comments","events_url":"https://api.github.com/repos/ipfs/js-ipfs/issues/1753/events","html_url":"https://github.com/ipfs/js-ipfs/issues/1753","id":387496548,"node_id":"MDU6SXNzdWUzODc0OTY1NDg=","number":1753,"title":"Don't drop idle connections + better connection state and events","user":{"login":"rmisio","id":794517,"node_id":"MDQ6VXNlcjc5NDUxNw==","avatar_url":"https://avatars1.githubusercontent.com/u/794517?v=4","gravatar_id":"","url":"https://api.github.com/users/rmisio","html_url":"https://github.com/rmisio","followers_url":"https://api.github.com/users/rmisio/followers","following_url":"https://api.github.com/users/rmisio/following{/other_user}","gists_url":"https://api.github.com/users/rmisio/gists{/gist_id}","starred_url":"https://api.github.com/users/rmisio/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rmisio/subscriptions","organizations_url":"https://api.github.com/users/rmisio/orgs","repos_url":"https://api.github.com/users/rmisio/repos","events_url":"https://api.github.com/users/rmisio/events{/privacy}","received_events_url":"https://api.github.com/users/rmisio/received_events","type":"User","site_admin":false},"labels":[{"id":103262945,"node_id":"MDU6TGFiZWwxMDMyNjI5NDU=","url":"https://api.github.com/repos/ipfs/js-ipfs/labels/question","name":"question","color":"cc317c","default":true},{"id":491319655,"node_id":"MDU6TGFiZWw0OTEzMTk2NTU=","url":"https://api.github.com/repos/ipfs/js-ipfs/labels/ready","name":"ready","color":"e6e6e6","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-12-04T21:54:08Z","updated_at":"2018-12-05T16:50:34Z","closed_at":null,"author_association":"NONE","body":"Noticing that connections seem to be dropped after some period of idleness. Actually noticing this in as little as one minute of no activity.\r\n\r\nObserved result:\r\n- connect to a circuit relay peer\r\n\r\n```\r\nnode.swarm.connect(addr, err => { ... });\r\n```\r\n\r\n- in less than a minute after connecting, send some messages to another web peer, which will only work if the relay connection are active on both sides.\r\n\r\n```\r\nnode.swarm.connect(differentAddr, err => {\r\n          if (err) { \r\n            return console.error(err);\r\n          }\r\n\r\n          node._libp2pNode.dialProtocol(differentAddr, protocol, (err, conn) => {\r\n            if (err) { \r\n              return console.error(err);\r\n            }\r\n\r\n            pull(\r\n              pull.once(someSerializedPayload),\r\n              conn,\r\n            );\r\n          });\r\n        });\r\n```\r\n\r\nAs long as the relay connections are active on both sides, the messages will fairly reliably go through. But, if the relay connection is dropped on either side, the messages won't go through. So...\r\n\r\n1.) Is it possible to configure a connection so it won't drop due to idleness?\r\n2.) Are there any events on a connection to let you know when the connection is no longer active?\r\n3.) Are there any methods on some type of connection object that would let you know the connection state? (connected or not connected)\r\n\r\n\r\nThe best work around I've been able to come up with up to this point is:\r\n\r\nA.) Every time I send a message, I first attempt to connect to the relay, which may already be connected, but hopefully the IPFS-JS code is smart enough to re-use the existing connection to spare the overhead of creating a new one each time.\r\nB.) Since both sides need to have an active connection with the relay, `A` won't account for the fact the the receivers connection has gone idle and been dropped. So, I suppose each side could on an interval try and reconnect, which isn't perfect, since unless you're going to have this interval running incessantly, there's still likely to be some time of a lack of connectivity. This is where a `disconnect` event would help, at least to a degree. \r\n\r\nWith that said, having the connection not disconnect unless you request it, would probably be ideal. This plus the additional connection state and events, so in the event it is dropped you could take proper action to reconnect and potentially keep your user aware of what's going on and why messages are failing."},"comment":{"url":"https://api.github.com/repos/ipfs/js-ipfs/issues/comments/444557477","html_url":"https://github.com/ipfs/js-ipfs/issues/1753#issuecomment-444557477","issue_url":"https://api.github.com/repos/ipfs/js-ipfs/issues/1753","id":444557477,"node_id":"MDEyOklzc3VlQ29tbWVudDQ0NDU1NzQ3Nw==","user":{"login":"jacobheun","id":639834,"node_id":"MDQ6VXNlcjYzOTgzNA==","avatar_url":"https://avatars3.githubusercontent.com/u/639834?v=4","gravatar_id":"","url":"https://api.github.com/users/jacobheun","html_url":"https://github.com/jacobheun","followers_url":"https://api.github.com/users/jacobheun/followers","following_url":"https://api.github.com/users/jacobheun/following{/other_user}","gists_url":"https://api.github.com/users/jacobheun/gists{/gist_id}","starred_url":"https://api.github.com/users/jacobheun/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jacobheun/subscriptions","organizations_url":"https://api.github.com/users/jacobheun/orgs","repos_url":"https://api.github.com/users/jacobheun/repos","events_url":"https://api.github.com/users/jacobheun/events{/privacy}","received_events_url":"https://api.github.com/users/jacobheun/received_events","type":"User","site_admin":false},"created_at":"2018-12-05T16:50:34Z","updated_at":"2018-12-05T16:50:34Z","author_association":"CONTRIBUTOR","body":"Ping is bundled by default in libp2p so you can call that directly https://github.com/libp2p/js-libp2p/tree/v0.24.2#libp2ppingpeer--options-callback. \r\n\r\n> 1.) Is it possible to configure a connection so it won't drop due to idleness?\r\n\r\nCurrently not, but we've been having discussions around Connection Management to request connections be held open. With 3 total peers in the mix during relays, even if you don't hang up the other two nodes could cut the connection. We're working on improving this logic in the future so the nodes can make better decisions on what to keep open.\r\n\r\n> 2.) Are there any events on a connection to let you know when the connection is no longer active?\r\nNot currently. libp2p-switch does keep stats for specific peers among other things `libp2pNode.stats`. The connection manager is looking at those right now, but the implementation is currently fairly basic. Peers can do some prioritization of importance with other peers via the connection manager as well, https://github.com/libp2p/js-libp2p-connection-manager#connmanagersetpeervaluepeerid-value. The problem with this is that it's only accounting for your priority. \r\n\r\n> 3.) Are there any methods on some type of connection object that would let you know the connection state? (connected or not connected)\r\n\r\nIn the latest version of libp2p, 0.24.x, this now exists. It should be bundled in the js-ipfs 0.34 release which should hopefully be out in the not too distant future. The libp2p update is currently in js-ipfs master if you want to experiment with that.\r\n\r\n[libp2p.dialFSM](https://github.com/libp2p/js-libp2p/tree/v0.24.2#libp2pdialfsmpeer-protocol-callback) will call back with a connection state machine instead of the normal connection. This enables you to hook into different events. For example:\r\n\r\n```js\r\nnode._libp2pNode.dialFSM(differentAddr, protocol, (err, connFSM) => {\r\n\r\n  connFSM.on('connection', (conn) => {\r\n    // Anytime a new stream is created for the connection, this will be called\r\n    // `conn` is a normal connection\r\n  })\r\n\r\n  // Called when the connection ends\r\n  connFSM.once('close', () => {})\r\n\r\n  // To close the connection when we're done\r\n  connFSM.close()\r\n\r\n  // Get the specific state the connection is in\r\n  connFSM.getState()\r\n\r\n})\r\n```\r\n"}}
{"actor":{"display_login":"jacobheun"}}