Skip to content
This repository has been archived by the owner on Sep 8, 2020. It is now read-only.

Commit

Permalink
Modify approach to use 'getstream' instead of 'stream-chat'
Browse files Browse the repository at this point in the history
Note: the addition of `(window as any).global = window;` to the polyfills.ts page is part of a solution for an "UncaughtReference error: global is undefined" error I was getting after importing 'getstream' to chat-service.ts. See comment section of angular/angular-cli#9827.
  • Loading branch information
Avery Koranda committed Mar 31, 2019
1 parent bda6a5a commit 3f24764
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 42 deletions.
50 changes: 8 additions & 42 deletions client/src/app/chat/chat-service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {StreamChat} from 'stream-chat';

import Credentials from '../../../credentials.json';

Expand All @@ -9,59 +8,26 @@ import {Observable} from 'rxjs/Observable';
import {environment} from '../../environments/environment';
import {Ride} from "../rides/ride";
import {User} from "../users/user";
import * as stream from 'getstream';

@Injectable()
export class ChatService {
readonly baseUrl: string = environment.API_URL + 'chat';
private API_KEY = (<any>Credentials).API_KEY;
private API_KEY_SECRET = (<any>Credentials).API_KEY_SECRET;
private chatClient = new StreamChat(this.API_KEY);


constructor(private http: HttpClient) {
this.setUser();
this.connectStream();
}

async setUser() {
console.log("API_KEY: " + this.API_KEY);
console.log("API_KEY_SECRET: " + this.API_KEY_SECRET);

console.log("disabling auth and permission checks");
// await this.chatClient.updateAppSettings({ disable_auth_checks: true, });

console.log("generating a token");
const token = this.getToken({ _id: "jlahey"}).subscribe( async (token) => {
console.log("token: " + token);

console.log("setting a user");
await this.chatClient.setUser(
{
id: 'jlahey',
name: 'Jim Lahey',
image: 'https://i.imgur.com/fR9Jz14.png',
},
token
);
console.log("user was set");

console.log("creating a channel");
await this.createRideChannel();
}); //this.chatClient.devToken('jlahey');
}

async createRideChannel(ride?: Ride) {
const channel = this.chatClient.channel('messaging', 'ride_id_here', {
name: 'Chat for ride going from ' + 'ride.origin' + ' to ' + 'ride.destination',
});

let state = await channel.watch();
async connectStream() {
const user = {
_id: 'avery'
};

const text = 'First test message is this. - Yoda';
const response = await channel.sendMessage({
text,
this.getToken(user).subscribe( userToken => {
const client = stream.connect(this.API_KEY, userToken,"49831");
});

console.log(response);
}

getToken(user: Object): Observable<string> {
Expand Down
2 changes: 2 additions & 0 deletions client/src/polyfills.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,5 @@ import 'zone.js/dist/zone'; // Included with Angular CLI.
* Need to import at least one locale-data with intl.
*/
// import 'intl/locale-data/jsonp/en';

(window as any).global = window;

0 comments on commit 3f24764

Please sign in to comment.