# @hmv007/chat-api-server

> Server for ready-to-go socket-chat-api

Latest version **1.0.4** (published 2021-05-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install @hmv007/chat-api-server
pnpm add @hmv007/chat-api-server
yarn add @hmv007/chat-api-server
bun add @hmv007/chat-api-server
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.4 |
| Published | 2021-05-23 |
| First published | 2021-05-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 12.3 KB |
| Known vulnerabilities | 0 (+6 in 2 direct dependencies) |
| Install scripts | no |
| Author | hmv007 |
| Maintainers | hmv007 |

## Links

- npm: https://www.npmjs.com/package/@hmv007/chat-api-server
- npm.io page: https://npm.io/package/@hmv007/chat-api-server

## Dependencies (7)

- [cors](https://npm.io/package/cors.md) ^2.8.5
- [bcrypt](https://npm.io/package/bcrypt.md) ^5.0.1
- [express](https://npm.io/package/express.md) ^4.17.1
- [mongoose](https://npm.io/package/mongoose.md) ^5.12.4
- [socket.io](https://npm.io/package/socket.io.md) ^4.0.1
- [jsonwebtoken](https://npm.io/package/jsonwebtoken.md) ^8.5.1
- [cookie-parser](https://npm.io/package/cookie-parser.md) ^1.4.5

## Recent versions

- 1.0.4 (latest) — 2021-05-23
- 1.0.3 — 2021-05-23
- 1.0.2 — 2021-05-23
- 1.0.1 — 2021-05-23
- 1.0.0 — 2021-05-23

## README

# 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!

---
_Source: https://npm.io/package/@hmv007/chat-api-server · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
