← All writing

4 min readPaymentsJavaScriptBrowser

Knowing when a popup is done, without polling

PayPal told our backend when a merchant finished linking, but the backend had no way to tell the open page. So the dashboard asks at the two moments that matter, when the PayPal tab closes or the merchant comes back, instead of polling.

Some flows have to happen on someone else’s website. Signing in with Google, paying with a wallet, connecting a PayPal account: the user leaves your page, does something on theirs, and comes back. Your page then has one question: are they done?

When we built PayPal linking for merchants, the server side of that question was solved. PayPal’s partner onboarding sends a webhook when a merchant finishes, and ours reached our backend. The gap was the last step: the backend knew, but the dashboard sitting open in the merchant’s browser didn’t, because we had no WebSocket or other way for the server to push anything to it.

The usual answers

  • Push it. Add a WebSocket or server-sent events, and let the server tell the page. That’s new infrastructure to run, secure, scale and reconnect, for one yes-or-no answer per linking attempt.
  • Poll. Have the page ask the status API every few seconds. It’s simple, but onboarding can take a merchant five minutes or more. Every five seconds for five minutes is 60 calls, nearly all answering “not yet”, multiplied by every merchant linking at the same time.

What we did: ask at the right moments

Nothing can change for the merchant while they’re busy on PayPal and not looking at us. It only matters at two moments:

  1. The PayPal tab closes. The dashboard opened that tab, so it holds a handle to it. It can’t see inside (another site), but it can always ask “are you still open?” That check happens in the browser and costs the server nothing.
  2. The merchant comes back to our tab. If they switch back to the dashboard with PayPal still open, they’re looking at the page, so it should be current.
const paypal = window.open(onboardingUrl, '_blank');

const watcher = setInterval(() => {
  if (paypal.closed) {
    clearInterval(watcher);
    refreshLinkStatus(); // ask our status API
  }
}, 1000);

window.addEventListener('focus', () => {
  if (!paypal.closed) refreshLinkStatus();
});

When the merchant finishes, PayPal sends them to our return page, and that page closes itself:

<script>window.close();</script>

A script can close a window that a script opened, so this works. The dashboard notices the tab is gone and asks once.

Why it’s nice

  • One or two status calls instead of dozens. The interval checks a browser property, not the server.
  • No new infrastructure. No socket server, no reconnect logic, no extra thing to monitor.
  • It feels instant. The merchant comes back and the dashboard is already up to date. No “I’m done” button to forget.
  • It’s tiny. A dozen lines, all in the page that started the flow.

Where it breaks

  • Closed doesn’t mean done. A merchant can close the tab halfway through. Treat a close as “go and check”, never as “success”. The status API decides, and “not linked yet, try again” is a normal outcome.
  • Focus fires a lot. Clicking into devtools, switching apps and back: each is a focus event. Debounce it, or skip the call if you checked a moment ago.
  • Popup blockers. Browsers block windows that aren’t opened in direct response to a click. Open the tab inside the click handler, not after an await.
  • Some sites make closed lie. A page can send a Cross-Origin-Opener-Policy header that cuts the link to the tab that opened it. Your handle can then report closed === true while the user is still signing in, which has broken other sign-in popups. The status check saying “not yet” handles that, which is one more reason never to treat closed as success. The focus check still catches the real finish later.
  • Mobile browsers. On phones, the user may switch apps and come back much later. Keep their session alive while they’re away, and stop watching after a reasonable maximum.

The options side by side

Approach How the page finds out Good for Watch out for
Push (WebSocket, SSE) The server tells the page Instant, many updates Infrastructure for one answer
Polling The page asks every few seconds Simple, no tab handle needed Mostly wasted calls, load grows with users
Tab close and focus The page asks when something could have changed Tiny, cheap, no backend work closed can lie; closing isn’t finishing
Message from the return page The return page sends postMessage to the opener Passing a result straight back Also broken by Cross-Origin-Opener-Policy; check origins

The webhook gave the server the truth. The browser events told the page when to ask for it. Between them, nobody had to build a socket for a single answer.

↑↓ move↵ openesc close