Created
February 21, 2020 08:14
Star
You must be signed in to star a gist
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
import { Component } from '@angular/core'; | |
import { StreamChat, ChannelData, Message, User } from 'stream-chat'; | |
import axios from 'axios'; | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.css'], | |
}) | |
export class AppComponent { | |
title = 'angular-chat'; | |
channel: ChannelData; | |
username = ''; | |
messages: Message[] = []; | |
newMessage = ''; | |
channelList: ChannelData[]; | |
chatClient: any; | |
currentUser: User; | |
async joinChat() { | |
const { username } = this; | |
try { | |
const response = await axios.post('http://localhost:5500/join', { | |
username, | |
}); | |
const { token } = response.data; | |
const apiKey = response.data.api_key; | |
this.chatClient = new StreamChat(apiKey); | |
this.currentUser = await this.chatClient.setUser( | |
{ | |
id: username, | |
name: username, | |
}, | |
token | |
); | |
const channel = this.chatClient.channel('team', 'talkshop'); | |
await channel.watch(); | |
this.channel = channel; | |
this.messages = channel.state.messages; | |
this.channel.on('message.new', event => { | |
this.messages = [...this.messages, event.message]; | |
}); | |
const filter = { | |
type: 'team', | |
members: { $in: [`${this.currentUser.me.id}`] }, | |
}; | |
const sort = { last_message_at: -1 }; | |
this.channelList = await this.chatClient.queryChannels(filter, sort, { | |
watch: true, | |
state: true, | |
}); | |
} catch (err) { | |
console.log(err); | |
return; | |
} | |
} | |
async sendMessage() { | |
if (this.newMessage.trim() === '') { | |
return; | |
} | |
try { | |
await this.channel.sendMessage({ | |
text: this.newMessage, | |
}); | |
this.newMessage = ''; | |
} catch (err) { | |
console.log(err); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is not working and showing the following errors.