Code Monkey home page Code Monkey logo

hyperswarm-seaport's Introduction

hyperswarm-seaport

p2p service registry and port assignment

NOTE - this is readme driven development. Features will be implemented when this note is removed. see spork for some idea how the p2p portions would be implemented.

Seaport makes it so you won't need to spend so much effort keeping configuration files current as your architecture grows to span many processes on many machines. Just register your services with seaport and then query seaport to see where your services are running.

To connect things together, you will need something taking care of the following roles:

  • server. Publishes a service with a named role@version (like a package.json specification)
  • client. Asks for a named service with a role@semver requirements (like a dependency)
  • registry. Manages connecting clients with servers.

These roles can be run in 3 processes spread across the internet, or combined together.

example

registry

First spin up a hyperswarm-seaport registry server

$ hyperswarm-seaport listen
 - Registry listening connect with pubkey whattzzuu5drxwdwi6xbijjf7yt56l5adzht7j7kjvfped7amova

server

const seaport = require('hyperswarm-seaport')
const pubkey = process.argv[2]
const ports = seaport.connect(pubkey)
const http = require('http')
const server = http.createServer((req, res) => res.end('beep boop\r\n'))
server.listen(ports.register('[email protected]'))

this will register a spork bound local port, the pubkey will be submitted to the seaport registry above.

client

next just get() that 'web' service

const seaport = require('hyperswarm-seaport');
const pubkey = process.argv[2]
const ports = seaport.connect(pubkey);
const request = require('request')
ports.get('[email protected]', (ps) => request(`http://${ps[0].host}:${ps[0].port}`).pipe(process.stdout))

for the client, the host (ps[0].host) will be localhost, with a spork activated local proxy that will connect to the service registered above

Result

the output of running all of this will be:

$ node server.js whattzzuu5drxwdwi6xbijjf7yt56l5adzht7j7kjvfped7amova &
[1] 6012
$ node client.js whattzzuu5drxwdwi6xbijjf7yt56l5adzht7j7kjvfped7amova
beep boop

and if you spin up client.js before server.js then it still works because get() queues the response!

hyperswarm-seaport's People

Stargazers

 avatar  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.