Skip to content

kokocan12/websocket-connection-using-shared-worker-example

Repository files navigation

Description

This is an example of websocket connection with server using shared worker.
Check out client/*.ts to grasp structure of websocket connection.

Introduce

1

Apps like github provide users with real-time reponses, I can see changes without refreshing the page when something in server is changed.

To implement this, polling or websocket are used.

Github uses websocket, but I can't see any websocket connections.

image

Where are the websocket connections?

SharedWorker

image

image

The websocket connections are maded in shared worker. You can see it at chrome://inspect/#workers

Why should I use SharedWorker when using a websocket connection?

When using a shared worker, all tabs and windows shared the context of worker. It means that even if many tabs are opened, the connection through the worker is made only once. It can reduce the load on the server.

A shared worker alone is not enough.

image

When many tabs are opened, The changes should be reflected into all tabs. Using BroadCastChannel makes this easier. BroadCastChannel sends a message to all tabs at the same time.

Conclusion

You get some advantages when making websocket connections with server using shared worker, Also the BroadCastChannel is good choice to reflect the change into all tabs.

The context of shared worker persists until all tabs are closed. Even if an additional tab is opened, it uses websocket connections that was maded before.

Demo

3

About

websocket connection using shared worker and broadcast channel

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published