Lurker News
Two browser windows (acting as socket clients) communicate their:

- Screen dimensions - (screen.width, screen.height)
- Window dimensions - (window.innerWidth, window.innerHeight)
- Window X/Y position - (window.screenX, window.screenY)
...or whichever calculation works best for you.
The original work by Bjorn Staal https://twitter.com/_nonfigurativ_/status/172732259457002734 used localStorage, but I found sockets more fun, because if tweaked a bit, this can be shared with friends.
Here's a demo of how it works and the codebase: https://github.com/Momciloo/fun-with-sockets/

Comments (129) 42 threads

Earlier comments
Filligree5 replies#
But… why does it lag? Shouldn’t this be trivial enough to be instant?
bawolff3 replies#
I wonder if it would be more instant if they used postMessage api instead of going over network.
4death41 reply#
Any local network usage should be nearly instantaneous.

My personal theory about the lag is that positioning the window is not synchronized to the JS thread, so the window moves before the JS thread is aware, which means you see the square move after the window has already moved.

bawolff#
Ah, my bad. I didn't read the code and just assumed there was a far away server.
rco8786#
Going over local network is more or less instant
taberiand1 reply#
Probably because of websocket network lag and also, in my anecdotal experience, window position updates are sometimes strangely jittery
mrits3 replies#
Why is it over a websocket? Can't it just share local data?
EGreg1 reply#
those were my thoughts too

You can use Broadcast Channels as well as Service Workers, even WebRTC would use a loopback interface!

Continue this thread — 1 reply

raincole#
I might be wrong, but I think the point is to simulate a server-client architecture (to show that it works even for an actual web app).
trevor-e#
It says so right in the OP

> The original work by Bjorn Staal used localStorage, but I found sockets more fun, because if tweaked a bit, this can be shared with friends.

akdor1154#
Background windows/tabs getting a lower priority maybe a factor.
Geee#
Querying window position is laggy unfortunately. It's a browser feature.
ogurechny#
Well, it's in a browser. Between trivial mouse move and trivial object coordinate change, there are numerous system and application layers to relay event to your interpreted (or compiled) code, and then to deliver state change to screen.

Even if we move to native world, making two different windows do the same instantly is not always trivial. For example, there have been articles about GUI toolkits making it impossible to have smooth window resizing. If we make everything ourselves (say, immediate drawing in response to WM_PAINT), we can only hope that the system notifies windows quickly, and that performance available is enough to pump bitmaps to screen. Or, better, we can try to use system-wide software or hardware compositor, add layers for individual objects, and only change their coordinates. Then we rely on compositor being good enough to handle 60/120/144/whatever updates per second when it's needed.

ww5201 reply#
That's a fun use of LocalStorage.

I used the same LocalStorage sharing technique to update the target window when the setting had been changed on a separate browser window. Just listen for the storage.onChanged event for the update.

abid786#
I don’t think this is using local storage. There’s a web sockets based server that is sending clients the location of the other boxes as they are moving
nikeee1 reply#
You can use a BroadcastChannel to send messages between the Windows directly. This should reduce latency when working locally.
oniony#
I love stollen. It's one of the few aspects of Christmas I actually look forward to.

Load more comments