Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting a timer for a long period of time warning #3054

Closed
rickardinho opened this issue Sep 14, 2017 · 49 comments
Closed

Setting a timer for a long period of time warning #3054

rickardinho opened this issue Sep 14, 2017 · 49 comments
Milestone

Comments

@rickardinho
Copy link

Hi, I'm getting the following warnings when using socket.io with my React Native project.

I'm on React Native version 0.44.3
and socket.io-client: 1.5.1

Any ideas of how to solve or is this a bug? It seems to be slowing my whole app down significantly, as well as producing lots of console warnings.

Here is the error in the console:

Setting a timer for a long period of time, i.e. multiple minutes, is a performance and correctness issue on Android as it keeps the timer module awake, and timers can only be called when the app is in the foreground. See facebook/react-native#12981 for more info.
(Saw setTimeout with duration 85000ms)
console.warn @ index.android.bundle:40843
setTimeout @ index.android.bundle:3412
Socket.onHeartbeat @ index.android.bundle:72517
Emitter.emit @ index.android.bundle:74290
Socket.onPacket @ index.android.bundle:72470
(anonymous) @ index.android.bundle:72336
Emitter.emit @ index.android.bundle:74290
Transport.onPacket @ index.android.bundle:73298
Transport.onData @ index.android.bundle:73294
ws.onmessage @ index.android.bundle:74952
dispatchEvent @ index.android.bundle:13268
(anonymous) @ index.android.bundle:12969
emit @ index.android.bundle:3853
__callFunction @ index.android.bundle:2092
(anonymous) @ index.android.bundle:1950
__guard @ index.android.bundle:2064
callFunctionReturnFlushedQueue @ index.android.bundle:1949
(anonymous) @ debuggerWorker.js:71
index.android.bundle:40843 Remote debugger is in a background tab which may cause apps to perform slowly. Fix this by foregrounding the tab (or opening it in a separate window).
console.warn @ index.android.bundle:40843
(anonymous) @ debuggerWorker.js:25
(anonymous) @ debuggerWorker.js:53

Thanks

@KingCosmic
Copy link

KingCosmic commented Sep 22, 2017

add {pingTimeout: 30000}to where you tie socket.io to your http server
example:

var io = socketio(server, {pingTimeout: 30000});
```  or some other amount it defaults to 85000 which is more then RN likes 

@ikhsanalatsary
Copy link

@KingCosmic not work for me

@KingCosmic
Copy link

Is your issue the same as the o e here? @ikgsanalatsary

@juergengunz
Copy link

does not work for me either

@matthieupinte
Copy link

Same warning... but not working for me either @KingCosmic :/
Thanks for your help ;)

@imdadahad
Copy link

Facing the same issue, any news?

@KingCosmic
Copy link

did you try configuring socket timeout

@matthieupinte
Copy link

@KingCosmic tried with { pingTimeout: 30000 }, { timeout: 30000 } as defined in doc. Do I have to do it server-side or client-side ?

@ikhsanalatsary
Copy link

@KingCosmic I tried both in server side & RN. But the yellow warning still show

@KingCosmic
Copy link

Why am I the only one be here ;-; @ikhsanalatsary what is your error exactly and @matthieupinte server side should be fine how are you connecting socket.io to your http server?

@ikhsanalatsary
Copy link

screenshot_2017-12-08-09-39-03

and in my React native app:
screen shot 2017-12-08 at 10 12 32 am

dependency:
"react": "16.0.0-alpha.12",
"react-native": "0.48.4",
"socket.io-client": "2.0.4",

server dependency:
"socket.io": "~2.0.3",

@KingCosmic
Copy link

@ikhsanalatsary set the ping timeout server side not client side and it should be fixed

@KingCosmic
Copy link

That's what fixed it for me anyways

@hansinhu
Copy link

does not work

@aprilmintacpineda
Copy link

on the server side do this:

const io = socketIO(server, {
  pingTimeout: 30000,
  pingInterval: 30000
});

@jussirantala
Copy link

(console as any).ignoredYellowBox = [ 'Setting a timer' ];

@clayrisser
Copy link

This fixes the yellow box and the console log. It even fixes it for Expo.

Simply place the following script at the beginning of your codebase.

import { YellowBox } from 'react-native';
import _ from 'lodash';

YellowBox.ignoreWarnings(['Setting a timer']);
const _console = _.clone(console);
console.warn = message => {
  if (message.indexOf('Setting a timer') <= -1) {
    _console.warn(message);
  }
};

@clayrisser
Copy link

I've encapsulated the previous logic into two npm modules, one for general node/javascript and one for react-native.

The react-native module is compatible with expo apps.

https://github.com/jamrizzi/ignore-warnings
https://www.npmjs.com/package/ignore-warnings
https://github.com/jamrizzi/react-native-ignore-warnings
https://www.npmjs.com/package/react-native-ignore-warnings

You would use it the following way . . .

import ignoreWarnings from 'react-native-ignore-warnings';
ignoreWarnings('Setting a timer');

@darrachequesne
Copy link
Member

As noted above, I think setting pingTimeout and pingInterval (which currently default to 60000 and 25000) should fix the issue.

@KingCosmic
Copy link

after some more looking around, pingTimeout fixed it on iOS but on android it still showed up but as said above pingInterval might fix that

@giantss
Copy link

giantss commented Mar 5, 2018

@ikhsanalatsary Hello, Can it be used in the react native project? I plan to use it. Please advise.

@clayrisser
Copy link

@giantss can what be used in the react native project?

@giantss
Copy link

giantss commented Mar 6, 2018

@jamrizzi socket.io

@ikhsanalatsary
Copy link

Its works after I update socket.io in the server and did

const io = socketIO(server, {
 pingTimeout: 30000,
 pingInterval: 30000
});

As @aprilmintacpineda mentioned

@giantss
Copy link

giantss commented Mar 6, 2018

@ikhsanalatsary Are you using socket.io-client in react native?

@ikhsanalatsary
Copy link

@giantss yes

@giantss
Copy link

giantss commented Mar 7, 2018

@ikhsanalatsary thanks, Could you provide some of the core client-side initialization code? Have you used webscoket?

@ikhsanalatsary
Copy link

const socket = io(api.BASE_API_URL, {
  transports: ['websocket'] // you need to explicitly tell it to use websockets
})

class RootContainer extends PureComponent<Props> {
  // class body
}

put it outside on your root component

@giantss
Copy link

giantss commented Mar 7, 2018

Your reply is very helpful to me, thank you very much.

@giantss
Copy link

giantss commented Mar 8, 2018

@ikhsanalatsary Hello, do you have encountered the following warning in the integration process?

Setting a timer for a long period of time, i.e. multiple minutes, is a performance and correctness issue on Android as it keeps the timer module awake, and timers can only be called when the app is in the foreground. See https://github.com/facebook/react-native/issues/12981 for more info.
(Saw setTimeout with duration 85000ms)

How do you solve this warning?

@clayrisser
Copy link

clayrisser commented Mar 8, 2018

@giantss I'm not sure what the real solution is yet, but you can at least hide the error for now. Refer to my previous comments.

@giantss
Copy link

giantss commented Mar 8, 2018

@jamrizzi Thank you for your answer, I have seen this solution before, I do not know what better solution

@clayrisser
Copy link

@giantss, yeah I wish I had a better answer.

@darrachequesne
Copy link
Member

darrachequesne commented Mar 8, 2018

@jamrizzi @giantss did you try the answer from @aprilmintacpineda above?

const io = socketIO(server, {
  pingTimeout: 5000 // this will be the default starting version 2.1.0 (currently it's 60000)
});

@giantss
Copy link

giantss commented Mar 9, 2018

@darrachequesne I tried this solution but it has no effect. @ikhsanalatsary He also encountered this warning. I don't know if he has solved it perfectly.

@aprilmintacpineda
Copy link

@giantss That's weird. Can you paste the code of your server.js here? at least the ones relevant to socket.io. That way we can help you more.

@giantss
Copy link

giantss commented Mar 9, 2018

@aprilmintacpineda Thank you for your advice.This warning appears in the react native app.

@aprilmintacpineda
Copy link

@giantss Yes, it does. Please post the server.js. Basically, the reason for the warning is that socket.io timer defaults to 80000 (I think, correct me if I'm wrong), react-native hates this. This is not an issue with socket.io-client, which is in the frontend, but rather this is an issue with socket.io, which is in the backend, and therefore, you need to fix it there.

@aprilmintacpineda
Copy link

aprilmintacpineda commented Mar 9, 2018

@giantss if you followed my advice above, you should have a code like this on the backend.

// .. other codes

server.listen(9000, () => {
  console.log('Dev server is running on http(s):https://localhost:9000.');
  console.log('http(s):https://10.0.22:9000 for android');
});

/**
 * socket.io
 */
import socketIO from 'socket.io';

const io = socketIO(server, {
  pingTimeout: 30000,
  pingInterval: 30000
});

// .. other codes

io.on('connection', socket => {
  // events
});

@giantss
Copy link

giantss commented Mar 9, 2018

@aprilmintacpineda I use the server.js in this demo,I tried to modify the fifth line of the demo server.js file to the following,

 io = require('socket.io')(server, {pingTimeout: 30000,pingInterval: 30000}),

then restart the server, the problem still exists.

@aprilmintacpineda
Copy link

have you done npm update?

@giantss
Copy link

giantss commented Mar 9, 2018

The version used in the demo is "socket.io": "^2.0.1",Do you think there is a relationship with the version?

@rafser01
Copy link

rafser01 commented Mar 10, 2018

Thanks @aprilmintacpineda
It works for me by adding

var io = require("socket.io")(server, {
  pingTimeout: 30000,
  pingInterval: 30000
});

in server side.

@ikhsanalatsary
Copy link

@giantss use latest version

@juergengunz
Copy link

juergengunz commented Mar 11, 2018

all solutions mentioned here, do not solve this issue for me.... except, hiding the warning...

@kyriakos
Copy link

Unfortunately I have no control over the server side so I can't change the timeout. Why is this a server side issue to start with though?
Is this warning safe to ignore?

@pentarex
Copy link

pentarex commented Mar 16, 2018

for whoever doesn't work, restart you server.... tried like 20 times... stupid me :D

@sunday58
Copy link

@codejamninja thanks..it fixed it for Android... Plz I want to know all the warnings this api can fix..thanks

@darrachequesne
Copy link
Member

For future readers: the default pingTimeout value was changed from 60000 to 5000 by this commit, included in [email protected] & [email protected].

So the complete delay (pingTimeout + pingInterval) is now 30s, which is below the RN threshold of 60s.

Note: this value may be a bit too small, as it may cause ping timeouts when uploading big files. Please increase it if needed.

Documentation here.

@darrachequesne darrachequesne added this to the 2.1.0 milestone Feb 22, 2021
darrachequesne added a commit to socketio/engine.io that referenced this issue Mar 2, 2021
This value was updated from 60000 to 5000 in [1], included in
`[email protected]` (Feb 2018).

The reasoning back then:

Some users experienced long delays between disconnection on the
server-side and on the client-side. The "disconnect" event would take a
long time to fire in the browser, probably due to a timer being
delayed. Hence the change.

That being said, the current value (5s) now causes unexpected
disconnections when a big payload is sent over a slow network, because
it prevents the ping-pong packets from being exchanged between the
client and the server. This can also happen when a synchronous task
blocks the server for more than 5 seconds.

The new value (20s) thus seems like a good balance between quick
disconnection detection and tolerance to various delays.

Note: pingInterval + pingTimeout is still below the threshold of React
Native, which complains if a timer is set with a delay of more than 1
minute.

[1]: 65b1ad1

Related:

- socketio/socket.io#2770
- socketio/socket.io#2769
- socketio/socket.io#3054
- socketio/socket.io#3376
darrachequesne added a commit to socketio/engine.io that referenced this issue Jun 6, 2022
This value was updated from 60000 to 5000 in [1], included in
`[email protected]` (Feb 2018).

The reasoning back then:

Some users experienced long delays between disconnection on the
server-side and on the client-side. The "disconnect" event would take a
long time to fire in the browser, probably due to a timer being
delayed. Hence the change.

That being said, the current value (5s) now causes unexpected
disconnections when a big payload is sent over a slow network, because
it prevents the ping-pong packets from being exchanged between the
client and the server. This can also happen when a synchronous task
blocks the server for more than 5 seconds.

The new value (20s) thus seems like a good balance between quick
disconnection detection and tolerance to various delays.

Note: pingInterval + pingTimeout is still below the threshold of React
Native, which complains if a timer is set with a delay of more than 1
minute.

[1]: 65b1ad1

Related:

- socketio/socket.io#2770
- socketio/socket.io#2769
- socketio/socket.io#3054
- socketio/socket.io#3376

Backported from 5a7fa13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests