Last active
October 30, 2019 09:55
-
-
Save pfrazee/7259f9201d44417c777803984715e1c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// control the session data on your connection | |
experimental.datPeers.getSessionData() | |
experimental.datPeers.setSessionData(obj) // obj must be no larger than 255 bytes when JSONified | |
// manage connected peers | |
var peers = experimental.datPeers.list() // list all peers connected to the current page's dat | |
var peer = experimental.datPeers.get(peerId) | |
await experimental.datPeers.broadcast(data) // send a message to all peers | |
experimental.datPeers.addEventListener('connect') // new peer | |
experimental.datPeers.addEventListener('disconnect') // peer closed connection | |
experimental.datPeers.addEventListener('message') // peer sent a message | |
experimental.datPeers.addEventListener('session-data') // peer set session data | |
// DatPeer object | |
peer.id | |
peer.sessionData | |
peer.send(data) // send a message to a peer | |
// all events: | |
event.peer // the sending DatPeer object | |
// message event: | |
event.data // string, object, or ArrayBuffer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note about this:
It's scoped to the current page's dat, which is intuitively similar to Single Origin Policy. So, if you're on
dat://fritter.hashbase.io
, then the peers you'll be seeing/messaging will be your peers for the Fritter site (dat://fritter.hashbase.io
).