/Chatter

A simple chat application using Socket io and MongoDB.

Primary LanguageJavaScript

API

Table of Contents

number

Require MongoDB native node client to keep chat history.

Parameters

string

Require Socket.io to use web sockets for the chat clients.

Parameters

connect

Connect to MongoDB from localhost to database- chatter.

err

If error in database connection - show error

  • Throws DatabaseError

log

If successfully connected, log connection.

on

Connect to Socket Client.

Parameters

on

Socket when recieving "add-user" call.

Parameters

on

Listen for send-message call from sockets.

Parameters

chat

Create or Use Collections

Parameters

join

Socket joins to rooms.

Parameters

  • null room "public"

username

insert

Insert into collections -users

Parameters

  • null-username string
  • null-joined_at timestamp timestamp

insert

Insert all chat activities to collection -chats

Parameters

  • username string username
  • message string content
  • towhom string recipient
  • sent_at timestamp timestamp

distinct

Fetch unique usernames from users collection

Parameters

Returns Array<srting> userlist

emit

Send the user list to clients.

Parameters

emit

Send chat history to connected sockets.

Parameters

emit

Emit chat activity to client.

Parameters

toArray

Fetch chat history of both public and private from DB.

Descending Sort,Limit to 10, change to array format for emit

Parameters

username

Send the incoming message to appropriate room

Parameters

  • if private - to intended user
  • if public - to all users

Returns emit new message with data

message