Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Websockets Work On Mobile Phones?

I'm currently thinking about creating a soft realtime mobile phone webapp, but when I started researching websockets, I found a load of scare stories about websocket connections dropping out on mobile phones:

WebSockets over a 3G connection

http://blog.hekkers.net/2012/12/09/websockets-and-mobile-network-operators/

Can this still be considered a problem?

Relatedly, I suspect a long polling client might be a good way to implement similar functionality, but wondered about the mobile specific issues I'm likely to encounter.

So far, I've read that long polling requests may have a considerable impact on battery life. I also hear that iOS somehow limits the number of connection made to a single server, which might be a problem.

Have any of you worked on a mobile application with a realtime component? And if you have, what challenges did you encounter, and how did you overcome them?

like image 474
Charlie Avatar asked Aug 15 '14 13:08

Charlie


People also ask

Can WebSockets be used in mobile?

You can decide to use any WebSocket-based protocol that supports Android. Regardless of your choice, though, you need to consider the entire spectrum of challenges you'll face when it comes to 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.

Do WebSockets work on iOS?

Apple has finally added Websockets as first-class citizen to its platforms. Websockets in iOS 13, macOS 10.15, tvOS 13, watchOS 6, and Mac Catalyst have gained first-class citizen status in networking stack. Apple has finally added support in URLSession and for lower level in Network.


1 Answers

I have built several websocket webapps with real-time data, and they perform very well on the iPhone and mobile. Websockets use a ping/pong connection to see if the connection is still alive. Things that have caused disconnection:

  • If you close down the app, then the connection will be dropped (on iOS webapps).
  • If the network does go down (wifi/3g/4g), then the connection will be dropped and not recover anything that was sent in that dropped time.

Considerations:

  • Write a simple reconnection routine into the onclose part of your javascript that tries to reconnect after a certain amount of seconds.

     function connect(){
         websocket = new WebSocket("wss://myws:5020");
         websocket.onclose=function(event){
             console.log(event);
             setTimeout(connect,5000); //re-connect after 5 seconds
             //..and so on
     }
    
like image 143
Entrabiter Avatar answered Oct 22 '22 19:10

Entrabiter