Skip to content

Instantly share code, notes, and snippets.

@kyle-ssg
Created November 17, 2016 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyle-ssg/37c3316cb7b19c2e963d63e6916808ca to your computer and use it in GitHub Desktop.
Save kyle-ssg/37c3316cb7b19c2e963d63e6916808ca to your computer and use it in GitHub Desktop.
***0. Firebase***
This application requires you to have firebase setup, from there you specify at what location you want to store chat
```
import FireChat from '../fire-chat';
const chat = new Firechat(db.ref("chat"));
```
***1. Register events***
Before interacting with FireChat it is best to register to the socket events, you likely only want to do this once per application.
```
chat.on(``'rooms-changed'``, (rooms)=>{
//{room1:{name"",id:"",lastMessage:{}}}
});
chat.on('message-add', (room, message)=>{
//{name:"Sender",message:"Hello World", typing:{user1:"Name"}}
});
```
***2. Get rooms***
Use an authorized firebase user and defined username to get rooms
*You will need to call this before receiving ``'rooms-changed'`` events*
```
chat.setUser(id, username);
```
You will receive an updated room collection from the rooms-changed event when the following events occur:
- setUser was successful
- The lastMessage changes
- You leave a room
- You enter a room
- A user is typing
***3. Enter room***
This will subscribe you to the room messages, and also make you a member of the room if you aren't already.
*you can only be subscribed into 1 room at a time*
```
chat.enterRoom(id, (room)=>{
//{name:"", id:""}
})
```
You will receive new messages via the ```message-add``` event for the room you are subscribed to
***4. Leave room***
This will remove the room from your list of rooms
```
chat.enterRoom(id, (room)=>{
//{name:"", id:""}
})
```
***5. Send typing notification***
Whenever you receive a keyup event call this, the lib will handle the rest
```
chat.userTyping(roomId)
```
***6. Read receipts***
Let the library know when the user is viewing a chat (e.g. chat is showing, app is active) and when the user isn't (e.g navigated away)
```
chat.startedViewing(roomId)
chat.stoppedViewing(roomId)
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment