Skip to content

Instantly share code, notes, and snippets.

@ayoisaiah
Created February 21, 2020 08:14
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ayoisaiah/a6890c51722bdb3c9682aabc4de5890f to your computer and use it in GitHub Desktop.
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);
}
}
}
@a7aseeb
Copy link

a7aseeb commented Sep 16, 2021

This code is not working and showing the following errors.

● app component ts - chatroom - Visual Studio Code 16-09-2021 13_13_38

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