WebSockets Energy Consumption

前端 未结 1 1387
挽巷
挽巷 2021-01-30 06:37

On a mobile device (iPhone/Android), what is the impact on battery consumption by implementing WebSockets vs HTTP?

Suppose I\'m building an instant messaging app and hav

相关标签:
1条回答
  • 2021-01-30 07:31

    Pure WebSockets/TCP sockets solution (without ping-pong packets) in idle state doesn't even detect a connection loss because it doesn't send any data. Once handshake has occured, it assumes that connection is established until something closes the socket or sender will attempt to send some data and it is detected that there is no connection anymore (in this case socket is sometimes closed automatically, but I wouldn't count on it).

    So, when connection is in idle state, the WiFi could go to sleep state without a worry and therefore minimize power consumption. But there is some drawback: it does not detect connection loss, so you have to implement ping-pong solution with attempt to reconnect if ping failed. The ping loop will have impact on CPU usage and power consumption, but it will be much lower than WiFi which is constantly in "On" state. Of course, you will lose the ability to receive data when in sleep mode.

    Source: own Android and WebSockets experience.

    0 讨论(0)
提交回复
热议问题