Skip to content

ipfs-examples/js-ipfs-browser-service-worker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IPFS in JavaScript logo

js-ipfs node using web workers

Using js-ipfs node in [SharedWorker][] from [ServiceWorker][]


Explore the docs · View Demo · Report Bug · Request Feature/Example

Table of Contents

About The Project

Getting Started

Prerequisites

Make sure you have installed all of the following prerequisites on your development machine:

Installation and Running example

> npm install
> npm start

Now open your browser at http://localhost:3000

Usage

In this example, you will find boilerplate code you can use to set up an IPFS node in a SharedWorker and use it from a ServiceWorker.

General Overview

src/main.js

Module is loaded in the main thread (DOM window) and is responsible for wiring all the pieces together:

  1. Activates a SharedWorker that runs an IPFS node.
  2. Registers a ServiceWorker to serve IPFS content from.
  3. Listens to MessagePort requests from the ServiceWorker and responds back with a MessagePort of the SharedWorker, enabling it to interact with shaerd IPFS node.

src/worker.js

Module is loaded in the SharedWorker. It demonstrates how to setup the IPFS node such that it can be used in other browsing contexts.

src/service.js

Module is loaded in the ServiceWorker and responds to all the requests from the page. It recognizes four different request routes:

  1. Routes /ipfs/..., /ipns/... are served html pages that:
  • Contain a full page iframe that has an src derived from request path e.g.:
/ipfs/Qm...hash/file/name -> /view/Qm...hash/file/name
  • src/main.js script loaded in it.

This way when request from /view/Qm..hash/file/name arrives ServiceWorker can obtain a MessagePort for the SharedWorker by requesting it from the iframe container.

  1. Routes /view/ipfs/... and are served corresponding content from IPFS. On such request message is send to an iframe container (That is why /ipfs/... and /ipns/... routes served iframe and src/main.js), through which MessagePort for the SharedWorker is obtained and used to retrieve content from the shared IPFS node and served back.

    There is a stub for /view/ipns/... route, which is left as an exercise for the reader to fill.

  2. All other routes are served by fetching it from the network.

Running example

You should see the following:

Screen Shot

If you navigate to the following address http://localhost:3000/ipfs/bafybeicqzoixu6ivztffjy4bktwxy6lxaxkvnavkya7kfgwyhx4bund2ga/ it should load a page from ipfs and appear as:

Screen Shot

For more examples, please refer to the Documentation

References

Documentation

Contributing

Contributions are what make the open source community such an amazing place to be learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the IPFS Project
  2. Create your Feature Branch (git checkout -b feature/amazing-feature)
  3. Commit your Changes (git commit -a -m 'feat: add some amazing feature')
  4. Push to the Branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Want to hack on IPFS?

The IPFS implementation in JavaScript needs your help! There are a few things you can do right now to help out:

Read the Code of Conduct and JavaScript Contributing Guidelines.

  • Check out existing issues The issue list has many that are marked as 'help wanted' or 'difficulty:easy' which make great starting points for development, many of which can be tackled with no prior IPFS knowledge
  • Look at the IPFS Roadmap This are the high priority items being worked on right now
  • Perform code reviews More eyes will help a. speed the project along b. ensure quality, and c. reduce possible future bugs.
  • Add tests. There can never be enough tests.
  • Join the Weekly Core Implementations Call it's where everyone discusses what's going on with IPFS and what's next