Skip to content

Commit

Permalink
Implement Firebase, chat is now Live
Browse files Browse the repository at this point in the history
  • Loading branch information
lirenyeo committed Jul 13, 2017
1 parent c1a99a7 commit 62cf4a9
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import ChatMessage from './chat_message'
import Firebase from './firebase'
import './App.css'

const chatRef = Firebase.database().ref('chat')

class App extends React.Component {
constructor(props) {
super(props);
Expand All @@ -13,9 +15,22 @@ class App extends React.Component {
this.sendMessage = this.sendMessage.bind(this)
}

componentWillMount() {
chatRef.limitToLast(40).on('child_added', (snapshot) => {
this.setState({
messages: this.state.messages.concat({
id: snapshot.key,
user: snapshot.val().dbUser,
text: snapshot.val().dbMessage
})
})
})
}

sendMessage(messageObj) {
this.setState({
messages: this.state.messages.concat(messageObj)
chatRef.push({
dbMessage: messageObj.text,
dbUser: messageObj.user
})
}

Expand Down

0 comments on commit 62cf4a9

Please sign in to comment.