npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

socketio-shared-webworker

v1.1.0

Published

SocketIO client running inside a shared WebWorker thread

Downloads

56

Readme

Socket.io inside a shared WebWorker

Running Socket.io in a shared webworker allows you to share a single Socket.io websocket connection for multiple browser windows and tabs. A drop in replacement for the socket.io client.

https://socket.io/ https://github.com/socketio/socket.io-client

Quick Install

npm i --save socketio-shared-webworker

Reason

  • It's more efficient to have a single websocket connection
  • Page refreshes and new tabs already have a websocket connection, so connection setup time is zero
  • The websocket connection runs in a separate thread/process so your UI is 'faster'
  • Cordination of event notifications is simpler as updates have a single source
  • Can be extended as a basis for IPC between your browser windows/tabs
  • It's the cool stuff..

Current Support

The aim is to support all methods from Socket.io client API. https://github.com/socketio/socket.io-client/blob/master/docs/API.md

All events to/from socket.io will be forwarded by the webworker.

Subscribe to events io.on('event', fn) is a local for each tab/window

Emit any event/obj io.emit('event', {data: 'blalba'}). Missing acknowledgement callback param atm.

Connect and disconnect io.emit('connect', fn) is broadcasted to all tabs/windows

Connection Manager io.Manager is not yet supported

var ws = wio('http://localhost:8000/')
ws.start() // use default SharedWorker script URL

// same as socket.io-client api: https://github.com/socketio/socket.io-client
ws.on('connect', () => {
    console.log('connected!')
    ws.emit('message', 'Hi There!')
})
ws.on('message', data => console.log('received message', data))
ws.on('disconnect', () => console.log('disconnected!'))
ws.on('error', data => console.log('error', data))

Install

Install locally using npm. (Alternatively clone the repo and look at index.html as an example)

npm install --save socketio-shared-webworker

To use in your nodejs project:

var wio = require('socketio-shared-webworker')
var ws = wio('http://localhost:8000/')
ws.start() // use default SharedWorker script URL

// same as socket.io-client
ws.on('connect', () => {
    console.log('connected!')
    ws.emit('message', 'Hi There!')
})

ws.on('message', data => console.log('received message', data))
ws.on('disconnect', () => console.log('disconnected!'))
ws.on('error', data => console.log('error', data))

To use in HTML wio is global.

<script src="socket.io-worker.js"></script>
<script>
var ws = wio('http://localhost:8000/')
ws.start() // use default SharedWorker script URL

// same as socket.io-client
ws.on('connect', () => {
    console.log('connected!')
    ws.emit('message', 'Hi There!')
})
ws.on('message', data => console.log('received message', data))
ws.on('disconnect', () => console.log('disconnected!'))
ws.on('error', data => console.log('error', data))
</script>

Using a specific SharedWorker script

When using ws.start() the default worker located in build/shared-worker-inline.js is used. This worker is served inline using URL.createObjectURL() instead of being served over HTTP. This is limited to Worker and SharedWorker since ServiceWorker requires the same domain. (ServiceWorker is not yet supported)

In order to specify the Worker script URL manually use:

ws.useWorker('http/same/domain/url/to/shared-worker.js')

Note: ws.useWorker('shared-worker.js') should point to the shared-worker.js url relative to your HTML page base URL. Shared webworkers can only be loaded from the same domain similar to CORS. When installed into a project with npm i socketio-shared-webworker you will find the script in: node_modules/socketio-shared-webworker/build/shared-worker.js

You may copy dist/shared-worker.js to your public/ directory and serve using express.static. An example of this is found in server.js. You can also serve it as a regular JS file with Apache, Nginx etc.

var wio = require('socketio-shared-webworker')
var ws = wio('http://localhost:8000/')
ws.useWorker('shared-worker.js') // use a specific SharedWorker script URL
// same as socket.io-client

Using a Worker instead of SharedWorker

By default the library will use Worker when SharedWorker is not available. If you want to specify using Worker specifically then use:

var wio = require('socketio-shared-webworker')
var ws = wio('http://localhost:8000/')
ws.setWorkerType(Worker)
ws.start()
// same as socket.io-client

At the moment only SharedWorker and Worker are supported. ServiceWorker is not.

Develop

git clone https://github.com/IguMail/socketio-shared-webworker
cd socketio-shared-webworker
npm install
# Start example/dev-server.js with HMR
npm run dev
# edit example/app.js, src/shared-worker.js, src/socket.io-worker.js etc.

In chrome visit the URL: chrome://inspect/#workers so see shared webworkers and inspect, debug. Visit the index.html in the browser for the demo.

Test

git clone https://github.com/IguMail/socketio-shared-webworker
cd socketio-shared-webworker
npm install
# Start development server with HMR
npm run dev
# Run unit tests
npm test 
# Run e2e tests
npm run test:e2e

Production build

npm run build

The builds will be placed in build/ directory. Copy these to your public/ directory in your server.

** To start the http and socket.io server to test the build **

npm start

Based heavily on

Thanks to.

https://gonzalo123.com/2014/12/01/enclosing-socket-io-websocket-connection-inside-a-html5-sharedworker/

https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers

https://www.sitepoint.com/javascript-shared-web-workers-html5/