npm package: @hmv007/chat-api-server
Description: Server for ready-to-go socket-chat-api
Author: hmv007
Latest version: 1.0.4

﻿# chat-api-server
An easy-to-use and ready-to-go chat-api-server in NodeJS. 

Inspired by a freelance project I was working on where I had to use Firebase just to have a chat feature in my app and the deadline was quite near to implement my own in NodeJS. Hence, we had to use Realtime database provided by Firebase.
In practise, implementing a Socket.io server is not tough but a little lengthy so here it is, a out-of-the-box socket server which can get your chat backend up-and-running within 10 secs.


# Documentation
1. Prerequisites
2. Usage
3. Conclusion

## Prerequisites
A MongoDB database is needed to save user sessions and messages. MongoDB Atlas is preferred.

## Usage
1. **Initializing the Server**
The chat-api-server uses an express server internally to run the server. Pass a PORT number and MongoDB Database URL in the initServer Function.

>     const  initServer = require('@hmv007/chat-api-server');
>     
>     // PORT = 5000
>     initServer(PORT, dbURL);
<br />

2. **Server logs**
Check the logs on the console. Server should be running on the given port and database must be connected. Check the logs for any errors.
<br />

3. **Server is up and running**
If no errors are found in the console your chat-api-server should be up-and-running!

***

### Pre-defined Events

#### Emitting Events
* ***joinRoom*** - Fire this event on the client side to join a room. room_id and user_id must be provided in the payload.
* ***sendMessage*** - Fire this event on client side to send a message. Payload must contain a "senderID" and "message".
<br />

#### Catching Events
* ***userOnline*** - Event fired when a user successfully joins the room. This event is broadcasted, i.e. all the users except the current user receive this event.
* ***inRoom*** - Event fired when a user successfully joins the room.  The current user only will receive this event on successfull joining of the room.
*  ***message*** - Event fired when a message was sent to the room and has been saved successfully. This event is broadcasted.
*  ***messageSaveSuccess*** - Event fired when a message was sent to the room and has been saved successfully. This event if for the current user.
*  ***messageSaveError*** - Event fired when a message was sent to the room but there was an error in saving the message to the database. Logs can be checked for exact error. Client side should handle the error accordingly.
<br />

#### Error event
* ***exception*** - Event is fired whenever an error occurs in socket requests. The payload is the error from MongoDB or custom error. The event should be catched while connecting to the socket server otherwise no errors will be visible on the client side. Server logs can also be checked for exact error.

***

## Conclusion
This is an initial build and subsequent builds will have new and improved features with the ability to login a user, authenticated socket routes, socket namespace, sending and receiving media files etc. These are just at the top of my head. 
Write me at hmv0071@outlook.com for suggestions and critics. 

Happy to help!