4

I am developing a browser extension for a real-time product. I have a background page with "persistent : true" set in the manifest.json (I am using version v2). I am continually polling the server for new data every second using setInterval(). The background script also caches the data it has gathered till present and gives it to any newly opened tab.

Things work fine until sometimes I noticed that when I put the computer to sleep for a long period, my poll to server just stops! If I refreshed any of the existing tabs, I do see cached data. This means, that the background page was not killed by Chrome. My question is, why is chrome just stopping the setInterval() call? Also, what is the correct way to revive the poll if it's stopped for some reason?

//relevant part of manifest.json
  "background": {
    "scripts": [
      "js/background/jquery.min.js",
      "js/background/bgconfig.js",
      "js/background/backgroundmanager.js",
      "js/background/eventsfetcher.js"
    ],

    "persistent": true
  },

Thanks!

Trunal Bhanse
  • 1,651
  • 1
  • 17
  • 27
  • 2
    Chrome isn't stopping it. Your OS is. – Travis J Aug 08 '13 at 21:19
  • 1
    @TravisJ: Thanks but what is the best way of reviving it? – Trunal Bhanse Aug 08 '13 at 21:20
  • 1
    Store the intervalID returned from `setInterval` in a global variable, then when the window gains focus again, clear that interval and run `setInterval` again. It seems there isn't really a way to check if intervals are still running, so that's why I'm suggesting that. Here's a link with information about checking if the window's gained focus: http://stackoverflow.com/questions/2720658/how-to-detect-when-a-tab-is-focused-or-not-in-chrome-with-javascript – Pluto Aug 08 '13 at 21:38
  • 1
    @Pluto: That was my last resort but seems like I will have to go with that. Thanks for your help! – Trunal Bhanse Aug 08 '13 at 22:38
  • 1
    Realtime? Have you considered using Websockets to reduce the network load? If not, at least use the [`chrome.idle`](https://developer.chrome.com/extensions/idle.html) API to detect that the user went away, in order to save resources when the user isn't there – Rob W Aug 09 '13 at 08:51
  • Thanks @RobW! Yes websockets is on the cards. This is really early in the product development so I want to get everything working based on polling first. Thanks a lot for the tip on chrome.idle api, seems pretty useful! – Trunal Bhanse Aug 09 '13 at 15:21
  • @Pluto will there be any issues when you keep background script "Persistent" property to "True"? – geet mehar Dec 16 '19 at 05:39
  • @geetmehar To be honest, at the time I made that comment, I must've not realized this question was for a Google Chrome extension as all my experience is with general web development. So basically, I have no clue. – Pluto Dec 17 '19 at 19:46

2 Answers2

6

According to the chrome documentation you should use the alarms API instead. I don't know if it will solve the issue but it's definitively worth trying!

I quote:

If your extension uses window.setTimeout() or window.setInterval(), switch to using the alarms API instead. DOM-based timers won't be honoured if the event page shuts down.

https://web.archive.org/web/20130715023501/http://developer.chrome.com/extensions/event_pages.html

=> https://developer.chrome.com/docs/extensions/reference/alarms/

Telmo Marques
  • 5,066
  • 1
  • 24
  • 34
Aegis
  • 1,749
  • 11
  • 12
  • 2
    Thanks but alarms API only lets me create alarms in minutes interval, I need to poll every second atleast. – Trunal Bhanse Aug 08 '13 at 21:41
  • 1
    well as I see the interval is defined as a double: did you try to put a fractional value in there? – Aegis Aug 08 '13 at 21:49
  • "In order to reduce the load on the user's machine, Chrome limits alarms to at most once every 1 minute but may delay them an arbitrary amount more." The real question is why OP needs to poll a server every second. – Teepeemm Aug 09 '13 at 00:57
  • http://developer.chrome.com/extensions/event_pages.html applies to **event pages**, but it's a background page instead with persistent: true. – 方 觉 Aug 09 '13 at 01:48
1

For anyone else looking for the solution, I ended up implementing what @Pluto suggested :

"Store the intervalID returned from setInterval in a global variable, then when the window gains focus again, clear that interval and run setInterval again"

This has worked well for me so far with no dropped events from server.

Trunal Bhanse
  • 1,651
  • 1
  • 17
  • 27