---
title: "Discussion on \"How I turned Local Storage into a Web Socket\" | Hashnode"
description: "Discussion on \"How I turned Local Storage into a Web Socket\". Enable real-time browser window communication using Local Storage to mimic WebSocket behavior"
url: "https://hashnode.com/posts/how-i-turned-local-storage-into-a-web-socket/64e1fc853f63dc4e93a101c4"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [How I turned Local Storage into a Web Socket](https://alemtuzlak.hashnode.dev/how-i-turned-local-storage-into-a-web-socket?utm_source=hashnode&utm_medium=feed)

Enable real-time browser window communication using Local Storage to mimic WebSocket behavior

Aug 20, 2023 · 8 min read · alemtuzlak.hashnode.dev  
Updated Sep 12, 2023  
Read the article

-   [#web-development](https://hashnode.com/tag/web-development)
-   [#reactjs](https://hashnode.com/tag/reactjs)
-   [#remix](https://hashnode.com/tag/remix)
-   [#webdev](https://hashnode.com/tag/webdev)
-   [#localstorage](https://hashnode.com/tag/localstorage)

## Written by

[Alem Tuzlak](https://hashnode.com/@Alle00)

@Alle00 · Sarajevo, Bosnia and Herzegovi

  
[View profile](https://hashnode.com/@Alle00)

Making your http://Remix.run experience more enjoyable. Creator of Remix Forge &amp; remix-hook-form &amp; remix-development-tools. OSS contributor and enthusiast.

## Responses 2

Markdown supported · ⌘↵ to post

[Kavir Kaycee](https://hashnode.com/@kavir)

[Sep 11, 2023](https://hashnode.com/posts/how-i-turned-local-storage-into-a-web-socket/64e1fc853f63dc4e93a101c4/comment/64ff85ec703e32bdbc1d11cc)

Very well written [Alem Tuzlak](https://hashnode.com/@Alle00)!

[Alem Tuzlak](https://hashnode.com/@Alle00)

[Sep 12, 2023](https://hashnode.com/posts/how-i-turned-local-storage-into-a-web-socket/64e1fc853f63dc4e93a101c4/comment/64ff85ec703e32bdbc1d11cc#reply-6500128bd3da8dcacfaa79d1)

Thank you [Kavir Kaycee](https://hashnode.com/@kavir) !

[Philip Birk-Jensen](https://hashnode.com/@BirkAndMe)

[Aug 22, 2023](https://hashnode.com/posts/how-i-turned-local-storage-into-a-web-socket/64e1fc853f63dc4e93a101c4/comment/64e46076b455f3cce04d097a)

Love the approach, I didn't know about the StorageEvent, so I dived in (maybe to deep).

Looking into it, it seems this is exactly the intended use of the StorageEvent ([developer.mozilla.org/en-US/docs/Web/API/Window/s…](https://developer.mozilla.org/en-US/docs/Web/API/Window/storage_event#specifications:) "it is really a way for other pages on the domain using the storage to sync any changes that are made").

\--

A peculiar thing is this only works for the localStorage, even though it should work for both local and session ([html.spec.whatwg.org/multipage/indices.html](https://html.spec.whatwg.org/multipage/indices.html#event-storage)).

The reason for this seems to be a catch-22, looking at the rules for the event: [html.spec.whatwg.org/multipage/webstorage.html](https://html.spec.whatwg.org/multipage/webstorage.html#storage-2:) "Dispatches a storage event on Window objects holding an equivalent Storage object". meaning it only fires on other windows (tabs), and not the instance making the changes.

The sessionStorage has a different instance for each open window ([html.spec.whatwg.org/multipage/webstorage.html](https://html.spec.whatwg.org/multipage/webstorage.html#introduction-15:) "If the user had multiple windows opened on the site, each one would have its own individual copy of the session storage object.").

So even if the event is fired on the sessionStorage, that exact instance isn't available in any other window, so there's no instance to dispatch to. I have not looked into any actual implementation here, so I don't know exactly what happens behind the curtain.

\--

Sorry for hijacking the comments, I just found it funny reading through the specs, and thought maybe others would appreciate the trivia.

Thanks for a great post!

[Alem Tuzlak](https://hashnode.com/@Alle00)

[Aug 22, 2023](https://hashnode.com/posts/how-i-turned-local-storage-into-a-web-socket/64e1fc853f63dc4e93a101c4/comment/64e46076b455f3cce04d097a#reply-64e4621ab455f3cce04d09d3)

This is spot on, I dived the same amount but decided to keep it out of the article. Plus the issue with sessionStorage (even if it worked like local) is that the data isn't persisted and most of the data i listen to needs to be! I'm glad you enjoyed the article!
