Skip to content

Instantly share code, notes, and snippets.

@ammezie
Created July 31, 2019 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ammezie/f3f8003d9b209d83e3a994aa9e88fff7 to your computer and use it in GitHub Desktop.
Save ammezie/f3f8003d9b209d83e3a994aa9e88fff7 to your computer and use it in GitHub Desktop.
// 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