Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A javascript library for IoT and networking

License

NotificationsYou must be signed in to change notification settings

asrient/airPeer

Repository files navigation

This is an implementation of AirPeer protocol in javascript for Node.js

About AirPeer Protocol

  • The main idea of the protocol is that our devices must be be able to discover andcommunicate with each other over any type network, be it theinternet, or localWiFi orbluetooth or evenUSB cables.
  • It is a networking protocol, like http or websockets, built on top oftcp sockets.
  • Peers connect with each others in ap2p fashion, without the need of a server.This means that, peers are able to communicate even without an internet connection.
  • It specifies a way of identification, discovery and sending data between peers over any kind of network.
  • It uses a broker server when connected over the internet. (As our devices cannot act as a public server behind a NAT like WiFi routers)
  • It provides aduplex 2 way mode of communication.
  • It is suitable for sending or receiving any amount of data, making it suitable for streaming.
  • Even though it is inspired by protocols like websockets and http it is not compatable with the same. It also adds a layer of identification and discoverablity along with communication.

Identification concepts

  • Every peer (i.e a program instance) first assigns itself a randomuid.
  • A peer can be uniquely identified by anairId.
  • AnairId consists of 3 parts:
    • uid as set by the peer.
    • host domain name (eg:air.example.com) of the broker server it is or shall connect to. (It is not needed to acutally connect to it to use this library)
    • sessionId a random string assigned by the library (On local networks) or by the broker server (On the internet)
  • airId formatuid:host:sessionId eg:657gh6fS8sHu:air.example.com:8jhtR5hgr
  • An airId without asessionId is called apeerId (uid:host)
  • A request can be sent to either anairId or apeerId. When sent to apeerId, all the peers with the same id will receive the request.
  • airIds are like dynamic IP addersses, they change over time, but apeerId always stays the same.
  • sessionIds therefore,airIds differ for each network interfaces, i.e yourairId on the internet is different from the local one.

How to identify a peer over a network

To know the airId of a known peer (ie peerId is known), we first send a message to peerId, ie all peers with the same peerId.From the responses received from those peers, you can decide which one is the actual peer and use theirairId directly for furthur communication with the peer.

You can also get a list of available local airIds for easy discoverablity when on a local network.

Communication between peers

There are 2 type of messages in airPeer.

  • Requests
  • Responses

Responses can be sent more than once to a particular request.

Usage

Initialize

constairPeer=require("../lib.js");// options: uid, host, appName, deviceNameairPeer.start("peer1","airbroker.herokuapp.com","testapp","my PC");

Send a request

// options: airId/peerId, message, callback with responseairPeer.request("sgtedrf5:example.com:45gf4","Hello there!",(res)=>{console.log(`A response from${req.from} arrived!`,res.body);});

A response object consists of:

  • body (type:buffer) message payload
  • status (type:Int) status code of the response
  • from (type:string)airId of the remote peer
  • parseBody() (type:func) parses the body tostring

Respond to a request

airPeer.on("request",(req)=>{req.parseBody();console.log(`A req from${req.from} arrived!`,req.body);if(req.body=="Hello there!"){// Send a responsereq.respond(200,"hi!");}// Send another responsereq.respond(200,"This is cool!");});

A request object consists of:

  • body (type:buffer) message payload
  • from (type:string)airId of the remote peer
  • parseBody() (type:func) parses the body tostring
  • respond() (type:func) parameters:statusCode,body

Get a list of local peers

varlocalPeers=airPeer.localPeers();console.log(localPeers);

list format:

[{uid:"we4vg4",host:"example.com",sessionId:"dfgyherty6b5",name:"my PC",app:"test-app",port:7657,address:"192.168.0.103",//IP adderss currently accessable by usaddresses:["192.168.0.103","172.16.0.23"],//All possible IP addersseslastSeen:1602135055,//Unix timestamp},];

If the peer is not connected,adderess will benull.

When a new local peer is discovered

You don't need to keep checking for new peers, instead you can

airPeer.on("localPeerFound",(rec)=>{varairId=rec.uid+":"+rec.host+":"+rec.sessionId;console.log("new peer found!",rec);console.log("sending request to",airId);airPeer.request(airId,"Hey! just found you",(res)=>{console.log("response received!",res);});});

Get my AirId

To get the current AirIds of the peer

var{ global, local}=airPeer.getMyAirIds();

[8]ページ先頭

©2009-2025 Movatter.jp