Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSockets Energy Consumption

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 have two options:

  1. Rely on Push Notifications to notify the device of a new message, and then fetch that message via a REST API.
  2. Establish a WebSocket connection when the user launches the app, and maintain that connection while the app is active. The server forwards all messages directly to the device, rather than using Push Notifications.

Someone told me that maintaining a persistent WebSocket connection would be a huge battery hog because it requires the antenna to be constantly active, rather than powering down after each request - but is that really true?

This answer suggests that on iOS, each device maintains a persistent connection with the Push Notification service at all times (similar to WebSockets, I suppose), so doesn't that suggest that the device's antenna is running 24/7 anyway?

The extra overhead of HTTP requests would be pretty insignificant for an instant messaging app, but in an application where a large number of items need to be downloaded constantly, a persistent WebSocket connection would be very useful. Any insight about maintaining long-term WebSocket connections on mobile devices (especially concerning battery consumption) would be extremely helpful!

like image 411
Hundley Avatar asked Mar 26 '15 15:03

Hundley


People also ask

Are WebSockets efficient?

Websockets are never less efficient than polling.

Is WebSocket more efficient than HTTP?

All the frequently updated applications used WebSocket because it is faster than HTTP Connection. When we do not want to retain a connection for a particular amount of time or reuse the connection for transmitting data; An HTTP connection is slower than WebSockets.

Why you should not use WebSocket?

Avoid using WebSockets if only a small number of messages will be sent or if the messaging is very infrequent. Unless the client must quickly receive or act upon updates, maintaining the open connection may be an unnecessary waste of resources.

Is WebSockets obsolete?

Websockets are largely obsolete because nowadays, if you create a HTTP/2 fetch request, any existing keepalive connection to that server is used, so the overhead that pre-HTTP/2 XHR connections needed is lost and with it the advantage of Websockets.


Video Answer


1 Answers

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.

like image 77
Paul Avatar answered Oct 08 '22 14:10

Paul