This repository contains the backend code for a Node.js chat application. This server is responsible for managing real-time communication between users, including private messages, group chats (rooms), and push notifications.
Before you can use this server, ensure you have the following software and dependencies installed:
- Node.js: Download Node.js
- npm (Node Package Manager): Included with Node.js installation
- Socket.io: A real-time engine for managing WebSockets
- Clone this repository to your local machine.
- Install the required Node.js packages by running
npm install
. - Create a
.env
file in the root directory and define your environment variables if needed (e.g., PORT). - Start the server by running
npm start
ornode your-server-file.js
. - Open a postman and visit
http://localhost:your_defined_port
. - You're all set! Now go build something awesome! 😎🚀
Socket.io is used to facilitate real-time communication between the server and connected clients. It allows for events to be emitted and received, enabling instant messaging and notifications.
The server keeps track of active users by storing their socket IDs in memory. When a user connects, their email is associated with their socket ID. This allows for private messages to be sent directly to specific users.
Users can send private messages to one another using their socket IDs. When a message is sent, the server locates the recipient's socket ID and sends the message directly to them.
The server supports the creation of chat rooms. Users can join a room, and the server keeps track of the users in each room. Room messages are broadcast to all members of the room except the sender. Users can also leave rooms, which triggers a notification to the remaining members.
The server supports push notifications. Users can send push notifications to all connected clients except the sender. These notifications can be used for broadcasting important messages or announcements.
- Connect to the server by specifying the server's URL, typically
http://localhost:5030
in your client application. - Emit events to interact with the server:
"join"
: To associate a user's email with their socket ID."sendMessage"
: To send a private message to a user by their email."join-room"
: To join a chat room."roomMessage"
: To send a message to a chat room."leave-room"
: To leave a chat room."push-notification"
: To send a push notification to all connected clients.
- Listen for events:
"message"
: Receive private messages."roomMessage"
: Receive messages from chat rooms."notification"
: Receive push notifications.
Make sure to adapt this server to your specific application's needs and add any necessary security features if deploying it in a production environment.
open-source code you can use it and also participate to improve it further.
I'm [Kamran], the developer behind this Node.js Chat Application Server. With a passion for real-time communication and web development, I created this server to provide a robust and flexible solution for chat applications.
If you have any questions, suggestions, or need assistance with this project, please feel free to contact me at [kamranaslam184@gmail.com].
You can find the code for this project in my Git repository: Link.