This provides a simple example of how to use ably-js from within a Web Worker, following the instructions in ably-js’s README.
It was written for manually testing the Webpack version 5 migration in ably/ably-js#1184, but I think serves as a generally useful example.
- Copy
.env.exampleto.env, and replaceinsert_key_herewith your Ably API key. - Run
npm install. - Run
npm run serve. This will launch a web browser.
You should see the following log messages in your browser’s console (the last two may appear in the opposite order):
Hello World, from worker!Attached to channelPublished to AblyGot message from Ably
These steps use npm pack, which emits a .tgz file similar to that uploaded by npm publish.
- Check out the
ably-jsrepository at the commit you wish to test. - At the root of the
ably-jsrepository, runnpm run build && npm pack --pack-destination <path>, where<path>is the directory you wish to output the.tgzfile to. - In the current repository, change the
package.json’sdependencies.ablyentry to"file:<tgz-file-path>", where<tgz-file-path>is the path to the.tgzfile output by the previous step. - Run
npm install.