I'm thinking about making a website that would end up being open in the background (or on a second screen) while someone is playing a video game. How could I pass a bounded key input to the website even though the browser is not currently focused? I'd like to avoid java applets but could do it if its the only way. Browser plugins might work too but the idea is to avoid any user installs and make it as simple as possible. Any thoughts?
-
What format is the game? A native app? Could you describe it a bit? – Rhys Mar 29 '14 at 21:45
-
Theoretically any game, but probably native installed game would be what its used for the most, so browser has no focus at all. – Hershizer33 Mar 29 '14 at 21:45
-
Do you want the browser to actually steal focus from the game, or just want a way to get data onto a page that's already open in a browser? – Rhys Mar 29 '14 at 21:46
-
I dont want to interrupt the game at all, so ideally no focus stealing. Like you said just a way to pass the input (bound key for instance) to a page that's already open in a browser. – Hershizer33 Mar 29 '14 at 21:48
2 Answers
I guess you only need that info when the page has actually focus.
- You could attach on the
onactivate
event of your HTML document and do an AJAX call then. Set the content of adiv
or something else according to the output; - Use Web Sockets to send content from your server to the client in the background.
You have to send the data to the server first to get this working.

- 153,850
- 22
- 249
- 325
-
Would this work if the browser had no focus at all (fullscreen native game is open like Battlefield 4)? – Hershizer33 Mar 29 '14 at 21:54
-
I don't get why you want it there before someone looks at it. You can download it as soon as the page has focus. – Patrick Hofman Mar 29 '14 at 21:56
-
And yes, this would work even if the page hasn't got focus (depending on the browser, mobile browsers often don't keep inactive tabs alive). – Patrick Hofman Mar 29 '14 at 21:57
-
-
Thanks a ton for following up! I actually haven't had a whole lot of time to work on this little side project this week as work has gotten pretty heavy. I really appreciate the help and will update this question when I get to try it out. – Hershizer33 Apr 03 '14 at 13:52
One way you might achieve this is by using WebSockets
(basic usage example).
A websocket will give you a messaging channel between the browser and your server. So for example, your game could call out to a REST
service hosted on your server that triggers a message to be sent down the websocket that your user's browser is connected to.
The basic steps might go something like this:
- User 1 logs into web page and websocket connects to server A.
- User 1 boots up game
- Game sends http request to REST service on server A for an action, let's say something like
<server_address>/move/east
- The server identifies user 1's websocket, and sends the 'move east' command down it
- User 1's browser picks up the message on the websocket, and updates the page appropriately
This wouldn't focus steal your game but would give you a relatively neat way of communicating between your game and a user's browser session. I think the hardest part of this might be what identifier you use to bind your user's game session to your user's browser session, but perhaps you already have some kind of login mechanism that would do this.
This wouldn't require any kind of plugins or 3rd party extensions as websockets are reasonably well supported in most browsers nowadays, but you'd have to write a bit of server-side code.

- 1,439
- 1
- 11
- 23
-
So the game would already exist. Like if the user is playing Battlefield 4 and they press a key we want something specific to happen on our website. – Hershizer33 Mar 29 '14 at 22:05
-
@Hershizer33 Ah, so the trigger doesn't come directly from the game itself? In that case, the `websocket` bit on its own would work if you just want to pass data every so often, or ajax as Patrick Hofman suggests. – Rhys Mar 29 '14 at 22:10