- number
- string
- connect
- err
- log
- on
- on
- on
- chat
- join
- username
- insert
- insert
- distinct
- emit
- emit
- emit
- toArray
- username
- message
Require MongoDB native node client to keep chat history.
null
string 'mongodb'
Require Socket.io to use web sockets for the chat clients.
Connect to MongoDB from localhost to database- chatter.
If error in database connection - show error
- Throws DatabaseError
If successfully connected, log connection.
Connect to Socket Client.
Socket when recieving "add-user" call.
Listen for send-message call from sockets.
Create or Use Collections
Socket joins to rooms.
null
room "public"
Insert into collections -users
null-username
stringnull-joined_at
timestamp timestamp
Insert all chat activities to collection -chats
Fetch unique usernames from users collection
null
string "username"
Returns Array<srting> userlist
Send the user list to clients.
Send chat history to connected sockets.
Emit chat activity to client.
Fetch chat history of both public and private from DB.
Descending Sort,Limit to 10, change to array format for emit
Send the incoming message to appropriate room
if
private - to intended userif
public - to all users
Returns emit new message with data