Skip to content

Instantly share code, notes, and snippets.

@ghsyeung
Created April 23, 2015 23:32
Show Gist options
  • Save ghsyeung/5697643d5f98f8ef77e1 to your computer and use it in GitHub Desktop.
Save ghsyeung/5697643d5f98f8ef77e1 to your computer and use it in GitHub Desktop.
/// <reference path="../../tools/typings/firebase/firebase.d.ts"/>
module Chat {
class ChatMessage {
constructor(public name:string, public text:string) {
}
}
export class ChatEngine {
private static baseUrl = "<Your Firebase URL>";
private firebase;
constructor(private name:string) {
this.firebase = new Firebase(ChatEngine.baseUrl);
}
send(message:string) {
this.firebase.push(new ChatMessage(this.name, message));
}
startReceive() {
this.firebase.on('child_added', (snapshot:FirebaseDataSnapshot) => {
this.displayMessage(snapshot.val());
});
}
displayMessage(message:ChatMessage) {
console.log("%c%s: %c%s", 'color: red', message.name, 'color: green', message.text);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment