Giter VIP home page Giter VIP logo

ipfs-pubsub-react-app's Introduction

IPFS PubSub Messaging Platform

This repository provides a messaging platform between two IPFS nodes using the Publish-Subscribe pattern, also known as 'pubsub'. IPFS (InterPlanetary File System) is a protocol and network designed to create a distributed and decentralized file system. The pubsub feature in IPFS enables greater network scalability and flexibility by allowing publishers to send messages classified by topic or content, and subscribers to receive only the messages they are interested in.

Prerequisites

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

Installation and Running the Example

With Node.js and git installed, install the project dependencies:

$ npm install

Start the example application:

$ node ./node1-api/serv.js
$ node ./node2-api/serv.js

In another terminal

cd node1-react-app

npm run start

In another terminal

cd node2-react-app

npm run start

1. Start two IPFS nodes

To demonstrate pubsub we need two nodes running so pubsub messages can be passed between them.

Right now the easiest way to do this is to install and start a js-ipfs and go-ipfs node.

2. Start the IPFS nodes

JS IPFS node

npm install -g ipfs
jsipfs init
# Configure CORS to allow ipfs-http-client to access this IPFS node
jsipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["http://127.0.0.1:8888"]'
# Start the IPFS node, enabling pubsub
jsipfs daemon

GO IPFS node

ipfs init
# Configure CORS to allow ipfs-http-client to access this IPFS node
ipfs config --json API.HTTPHeaders.Access-Control-Allow-Origin '["http://127.0.0.1:8888"]'
# Start the IPFS node, enabling pubsub
ipfs daemon --enable-pubsub-experiment

In the "Node to connect to" field enter /ip4/127.0.0.1/tcp/5001 in one browser and /ip4/127.0.0.1/tcp/5002 in the other.

This connects each browser to an IPFS node and now from the comfort of our browser we can instruct each node to listen to a pubsub topic and send/receive pubsub messages to each other.

N.B. Since our two IPFS nodes are running on the same network they should have already found each other by MDNS. So the "CONNECT TO PEER" field is not required. If you find your pubsub messages aren't getting through, check the output from your jsipfs daemon command and find the first address listed in "Swarm listening on" - it'll look like /ip4/127.0.0.1/tcp/4002/ipfs/Qm.... Paste this address into the "Peer to connect to" field for the browser that is connected to your go-ipfs node and hit connect.

Finally, use the "Subscribe To" and "Message to send" fields to do some pubsub-ing, you should see messages sent from one browser appear in the log of the other (provided they're both subscribed to the same topic) by clicking on Call API button.

Code Execution

Clip:

alt text

Code Breakdown

The node1-api has serv.js file that contains functions:

  • function nodeConnect to create IPFS node and connect it to the browser
  • function peerConnectfor peer discovery and connection
  • function subscribe takes a topic as an argument and uses pubsub.subscribe to recieve messages relate to the topic.
  • function send takes message as and uses pubsub.publish to send messages related to the topic.

The node2-api has similar code breakdown.

Conclusion

The exchange of messages between Node1 and Node2 on topic of common interest is successfully implemented by the use of PubSub.

ipfs-pubsub-react-app's People

Contributors

aryamanraj avatar aryamanxlighthouse avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.