1

I have a client-side Golang application running on my machine. I also have a browser open, and in that browser there might be a tab running my web application (which is completely separate from the Golang app).

From the Golang app, I would like to programmatically refresh the browser tab (and maybe if possible, bring it to front, but that's less important).

I researched quite a lot already, and I concluded this is not possible just by communicating to the browser, there is no standard (especially cross-platform and cross-browser) interface with which we can trigger the refresh of a specific tab of a browser.

So I suppose I'll need to have some custom JS code running on the website with which my Golang application can communicate and trigger the refresh of the tab.

What's the easiest way to do this?

(I was looking at livereload.js and lrserver, but these all start with the premise that there is a folder of content we'd like to watch and automatically reload on any change. But I don't want that, I just programmatically want to trigger the refresh. Also, this Golang app is not hosting the website, it's just a separate client-side application.)

Mark Vincze
  • 7,737
  • 8
  • 42
  • 81
  • Do you want to refresh the page on some event at server side? – Ankit Deshpande Oct 04 '16 at 18:49
  • No, I don't want to include the web server in this story at all. (Imagine that it's not even a site coming from a server, it might be just local static html+js.) I want to programmatically refresh the browser tab from my Golang application running on the same machine as the browser. – Mark Vincze Oct 04 '16 at 18:59
  • 3
    you can't do it. best you can do is have browser javascript polling a server endpoint /shouldIReload, or have browser javascript make that decision itself and use one of these techniques http://stackoverflow.com/questions/5404839/how-can-i-refresh-a-page-with-jquery/5404869#5404869 – Plato Oct 04 '16 at 19:25
  • 1
    maybe you can push the serverside data with a websocket to the page in question. – RickyA Oct 04 '16 at 19:29
  • @Plato Well that's not out of question. In the Golang app I can host a little local http server on some predefined port, and the JS in the browser can communicate with that. So I could probably come up with a completely custom solution, I was just wondering if there is already a concise example or library for this. – Mark Vincze Oct 04 '16 at 19:30
  • 1
    You may program a websocket endpoint on your local http server. Then you should use Javascript to connect to that endpoint and wait for the "refresh trigger" there. – Koala Yeung Oct 05 '16 at 08:25

1 Answers1

2

As suggested by some comments, there seems to be no API through which we could connect to a browser from Golang, query the list of tabs, and refresh a particular page (at least not in a cross-browser and cross-platform way).

One possible approach to do this is to host a small WebSocket endpoint in Golang, and connect to it from the site we want to refresh. Then send a message through the WS connection every time we want to reload the site, and in JavaScript call location.reload() when we receive the message.

I described all the details in a blog post, and uploaded a complete working example to GitHub.

Mark Vincze
  • 7,737
  • 8
  • 42
  • 81