This document explains how to use the server code of the Electron.js chat app to create a mobile version of the app using Flutter. The server code is written in Node.js and utilizes the Express framework and Socket.IO for real-time communication.
The server provides the following functionalities:
- User registration and login
- Notification handling
- Real-time messaging
- Checking user and contact statuses
The server runs on localhost:3000 by default.
- Node.js and npm must be installed.
- Install dependencies by running the following command in the server directory:
npm install express socket.io cors
- Start the server using:
node server.js
Description: Returns a simple "Hello World!" message.
Description: Registers a new user.
- Request Body:
{ "name": "User Name", "username": "unique_username", "password": "secure_password" }
- Response:
{ "me": { "id": 1001, "username": "unique_username", "name": "User Name", "password": "secure_password", "notifications": [] }, "contacts": [] }
Description: Logs in an existing user.
- Request Body:
{ "username": "unique_username", "password": "secure_password" }
- Response:
{ "id": 1001, "username": "unique_username", "name": "User Name", "password": "secure_password", "notifications": [] }
Description: Checks if a username is available.
- Query Parameter:
username
(string) - Response:
{ "available": true }
Description: Checks the online status of a user by their ID.
- Query Parameter:
id
(number) - Response:
- If online:
"online"
- If offline:
"offline"
- If online:
Description: Retrieves the IDs of the contacts added by a user.
- Query Parameter:
id
(number) - Response:
[1002, 1003]
Description: Retrieves details of a specific contact by their ID.
- Query Parameter:
id
(number) - Response:
{ "id": 1002, "name": "Contact Name", "username": "contact_username", "messages": [], "haveUnreadMessages": false }
Description: Sends a notification to a user.
- Request Body:
{ "receiverId": 1002, "sender": { "id": 1001, "name": "User Name" }, "type": "Request", "status": "Pending" }
- Response:
true
- Server Address:
http://localhost:3000
- Use Socket.IO for real-time communication.
- Authentication: Send user details in the handshake
auth
object.
-
Connect: Establishes a connection to the server.
- If the user has undelivered messages, they are sent immediately.
-
Message Event: Sends a message to another user.
- Event Name:
message
- Data Format:
{ "to": 1002, "message": "Hello!", "timestamp": "2024-12-12T10:00:00Z" }
- Event Name:
-
Notification Event: Receives real-time notifications.
- Event Name:
notification
- Data Format: Same as the notification endpoint.
- Event Name:
-
Disconnect: Updates the user's online status.
- HTTP Requests: Use libraries like
http
ordio
in Flutter to make API calls. - Socket.IO: Use the
socket_io_client
package for real-time communication:import 'package:socket_io_client/socket_io_client.dart' as IO; IO.Socket socket = IO.io('http://localhost:3000', <String, dynamic>{ 'transports': ['websocket'], 'auth': { 'user': { 'id': userId, 'username': username, 'password': password } } }); socket.on('connect', (_) { print('Connected'); }); socket.on('message', (data) { print('New message: $data'); });
- Ensure that the server is accessible to the mobile app by hosting it on a public IP or configuring port forwarding.
- Use secure methods like HTTPS for production.