Skip to content

Instantly share code, notes, and snippets.

@pfrazee
Last active October 30, 2019 09:55
Show Gist options
  • Save pfrazee/7259f9201d44417c777803984715e1c4 to your computer and use it in GitHub Desktop.
Save pfrazee/7259f9201d44417c777803984715e1c4 to your computer and use it in GitHub Desktop.
// 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
@pfrazee
Copy link
Author

pfrazee commented Jun 6, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment