advanced SharedWorker usage examples
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
websocket example
shared worker.js


These are two advanced examples of using an Html 5 feature called "SharedWorker".
A SharedWorker is a thread, which runs in your site browsing session.
It's like a server side session, but on the client side.
Using this thread you can also communicate between several open windows of one website.
If you close all open windows of the website, and open them again - this will start a new session.

SharedWorkers can be used, for example, when you have a websocket connection to server-side on each page (for example, for "push" notifications).
Having a dedicated websocket connection on each tab could be resource-consuming and time-consuming, 
but if you use a SharedWorker, you can create a single websocket connection and reuse it between all the tabs.

These examples also use the library I wrote: "shared worker.js".
It greatly simplifies usage of this feature.

The examples must be run from a webserver - they won't work if you just open them from your disk drive (this is the bug in Chrome).

The first example is simple.
The second example shows how you can have only one websocket connection shared between multiple tabs.
To run it, first do "npm install ws", and then "node server.js" (from the example folder).