-
-
Save ammezie/f3f8003d9b209d83e3a994aa9e88fff7 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
// js/components/AdminChat.vue | |
<script> | |
import { StreamChat } from 'stream-chat'; | |
export default { | |
data() { | |
return { | |
modal: { | |
show: false, | |
}, | |
message: '', | |
messageData: [], | |
collapsed: false, | |
channel: null | |
} | |
}, | |
computed: { | |
username() { | |
return "admin" | |
} | |
}, | |
mounted() { | |
this.initializeClient(); | |
this.createChannel(); | |
}, | |
methods: { | |
async createChannel(){ | |
const {data} = await axios.post('/getChannel', { | |
from_username: "admin", | |
to_username: "client", | |
from: "admin", | |
to: "client", | |
}) | |
const channel = this.client.channel('messaging', data.channel, { | |
name: 'LiveChat channel', | |
members: ["admin", "client"] | |
}); | |
this.channel = channel | |
channel.watch().then(state => { | |
this.messages = state.messages | |
channel.on('message.new', event => { | |
this.messageData.push(event.message) | |
}); | |
}) | |
}, | |
async initializeClient () { | |
const {data} = await axios.post('/generate-token', { | |
name: "admin" | |
}) | |
const client = new StreamChat(process.env.MIX_STREAM_API_KEY); | |
await client.setUser( | |
{ | |
id: "admin", | |
name: "admin", | |
}, | |
data.token, | |
); | |
this.client = client | |
}, | |
sendMessage() { | |
this.channel && this.channel.sendMessage({ | |
text: this.message | |
}); | |
this.message = ""; | |
}, | |
toggleModal() { | |
this.modal.show = !this.modal.show; | |
}, | |
showModal() { | |
this.modal.show = true; | |
}, | |
hideModal() { | |
this.modal.show = false; | |
}, | |
} | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment