Last active
June 14, 2022 08:56
-
-
Save jabis/bba88d0c59c4587a94a3536bd4721749 to your computer and use it in GitHub Desktop.
Send and receive messages with gun
This file contains hidden or 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
/*so let's simplify you have | |
- your letters (all messages, yours & other peer) which you draw to DOM | |
- you have your outbox , when you say something, that the other person listens to with .on(...) | |
and adds to their letters when they arrive | |
- you listen with .on(...) the other peers messages and add them to letters when | |
they arrive | |
- on your send(){...} you would add the message to your letters | |
and to the outbox so your message can trigger the other persons .on */ | |
class Chat { | |
async send(to,input){ // to: others' pubkey to save messages and his own outbox to subscribe to | |
//requirements who and me | |
const me = gun.user(); | |
const toData = await gun.user(to).once(); | |
console.log(toData); | |
if(me.is){ | |
const mypub = me._.sea.pub; | |
//let input = input | |
const time = Gun.time.is(); | |
let msg = {from:me.is.alias,to:toData.alias,input:input,time:time}; | |
console.log(msg) | |
// Note we are storing stuff only in our user space gun.user().get... | |
me.get("nasa/"+to).get("outbox").get(time).put(msg) // This is what the other peer is listening in `.on` in get method | |
me.get("nasa/"+to).get("letters").get(time).put(msg) // Store my sent messages in nasa/counterpart.letters, This is for your DOM printing - a list | |
return Promise.resolve("OK"); | |
} | |
return Promise.reject("whoops") | |
} | |
async listenTo(who){ | |
const me = gun.user(); | |
if(me.is) { //make sure we're authenticated | |
const mypub = me._.sea.pub; // might want to test this one too so we don't throw | |
let i = 0; | |
return gun.user(who).get("nasa/"+mypub).get("outbox").map().on( (data,field) => { // subscribe to other peers my pubkey | |
console.log('data',data,'field',field); | |
let msg = {from:data.from,input:data.input,time:data.time,to:data.to}; | |
console.log(data,"message",msg,'field',field) | |
if(field && data.from && data.input && data.time && data.to) gun.user().get('nasa/'+who).get("letters").get(field).put(msg) //put this in my **user** scope nasa/who.letters | |
}) | |
} | |
} | |
async getToDom(from){ | |
let container = document.getElementById('#messageList'); | |
const me = gun.user(); | |
const fromUser = await gun.user(from).once(); | |
return me.get('nasa/'+from).get("letters").map().on((letter,field) => { | |
// insert to dom via createElement or something | |
console.log(letter,"fromUser.alias equals to letter.from", letter.from == fromUser.alias); // maybe hilight other users text | |
console.log("letter from is my alias?", me.is.alias == letter.from) // maybe do stuff with it, like enable editing? | |
console.log("from", letter.from, "to", letter.to, "input", letter.input,"time", letter.time); | |
}) | |
} | |
async getLetters(from){ | |
//utility function to get once all messages for pubkey who | |
return gun.user().get("nasa/"+from).get('letters').once(async (messages) => { | |
return messages; | |
}); | |
} | |
} | |
export default new Chat() | |
// this was created with the help of the legend @jabis on discord |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment