7

Is it possible to keep a html 5 web pages WebSocket connection open in Mobile-Safari once the screen is locked?

I want to send my users continuous updates throughout the day and it seems silly that their screens should always have to be unlocked to receive those notifications.

Are there any other options?

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
TrevTheDev
  • 2,616
  • 2
  • 18
  • 36
  • what is the server side architecture are you using? – khagesh Aug 02 '14 at 09:39
  • @Pinal: please don't add inline code spans (`like this`) for emphasis. See [this Meta post](http://meta.stackexchange.com/questions/135112/inline-code-spans-should-not-be-used-for-emphasis-right/165710#165710) for more information. – Qantas 94 Heavy Aug 04 '14 at 13:06
  • @khagesh: I'm not sure server architecture matters? I'm using rails 4 with the rails-websocket gem. – TrevTheDev Aug 05 '14 at 02:29

2 Answers2

9

I don't think it's possible to keep the connection open while the browser is in the background, or when the screen is locked, the reason being that the app is essentially frozen in memory. Here's a quote from a similar question:

the reason you cant keep a network socket open, is that without your app jumping to the foreground when it receives a connection, it cannot respond to network traffic(because if it is not in the foreground its memory content is frozen).

However, I did find this page on Push Notifications for Websites that shows you 'how to sign up your users to receive notifications even when your site is not running in Safari'.

There are some other options: if you want to send continuous updates, you could write an app and either follow the instructions on Apple's site to keep a socket open permanently, or you could configure the app to implement Push Notifications.

I'm sorry I couldn't find a quick fix, but I hope at least one of these options works for you!

Community
  • 1
  • 1
Eoin
  • 833
  • 2
  • 13
  • 24
  • Thanks Eoin. I strongly suspect you are right that one cannot keep a socket open in mobile safari. I did see some hacks to keep javascript running in a safari backgrounded page - which may present a solution but I have not yet tried it out. My last resort is to not use Safari and switch to a native wrapper such as Cordova which I was hoping to avoid. – TrevTheDev Aug 05 '14 at 02:23
  • 1
    This answer is correct. there is no way for the simple reason of the tabs/pages are run in like VM's once there minimized think of the VM being suspended, nothing can interact with them witch is correct a web site should not be running stuff when your not on the page. – Barkermn01 Aug 05 '14 at 10:49
  • Thanks Martin. Having the "VM" which has the focus suspend itself simply because the screen blanked out seems a bridge to far especially if the device (1) has power and (2) is on wifi. It seems it would be better if they allowed developers an option to not have the VM suspended if the screen blanks out. – TrevTheDev Aug 06 '14 at 03:52
4

I have found a hacky way to keep WebSocket alive in Mobile Safari.

Basically it's the same solution as for this question.

Create an infinity looping audio file to keep Javascript running:

<audio loop src="http://www.sousound.com/music/healing/healing_01.mp3"></audio>

Note: some user interaction is required to initiate the audio file.

It would be nice if a WebSocket kept the browser alive in the same manner as an audio or video file.

PS this also works on Android.

Community
  • 1
  • 1
TrevTheDev
  • 2,616
  • 2
  • 18
  • 36
  • I find it unfair! Imagine a situation where lot of websites included WS and kept it in the background. That would drain the battery without user consent. This is what I am seeing in the Lightning browser on Android, it even keeps the connection after the phone is locked, argh! – k3a Aug 01 '15 at 18:56
  • 3
    @k3a what I find is unfair is that a native app can keep its thread alive whilst a web app can not - especially when using ws the whole point is real time updates! A better solution would be for Android to ask the user to allow this to occur. – TrevTheDev Oct 09 '15 at 09:32
  • This [no longer works](http://stackoverflow.com/questions/9709891/prevent-ios-mobile-safari-from-going-idle-auto-locking-sleeping) mobile safari – Ryan Harmuth May 26 '16 at 12:33