A robust and scalable RESTful API for building real-time chat applications powered by Laravel. This project provides the backend infrastructure for messaging systems, enabling seamless communication between users with features like real-time messaging and notifications, user authentication, and message history.
- Real-Time Messaging: Leverages WebSockets (via Pusher) for instant message delivery.
- User Authentication: Secure user registration and login using Laravel Sanctum.
- Message History: Stores and retrieves chat history using a relational database (MySQL).
- RESTful Endpoints: Well-defined API endpoints for sending, receiving, and managing messages.
- Scalable Architecture: Designed to handle high concurrency and large-scale deployments.
- Modular Design: Easily extendable for custom features like group chats, file sharing, or chatbots.
- Real-Time Notifications: Notify users of new messages or events in real time using OneSignal.
- Cross-Platform Compatibility: Works seamlessly with web, mobile, and desktop clients.
- Built with Laravel 10.x for robust backend development.
- Uses Pusher for real-time communication.
- Implements OneSignal for push notifications.
- Implements RESTful API standards for easy integration.
- Supports MySQL for reliable data storage.
- PHP 8.1 or higher
- Composer (for dependency management)
- MySQL 5.7 or higher
- Node.js and NPM (for front-end assets, if applicable)
- Pusher account (for real-time messaging)
- OneSignal account (for push notifications)
-
Clone the Repository:
git clone https://github.com/zakigit1/Chat-App-Laravel-API.git cd Chat-App-Laravel-API
-
Install Dependencies:
composer install npm install
-
Configure Environment:
-
Copy
.env.example
to.env
:cp .env.example .env
-
Update
.env
with your database, Pusher, and OneSignal credentials:DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=chat_app DB_USERNAME=root DB_PASSWORD= BROADCAST_DRIVER=pusher PUSHER_APP_ID=your-pusher-app-id PUSHER_APP_KEY=your-pusher-app-key PUSHER_APP_SECRET=your-pusher-app-secret PUSHER_APP_CLUSTER=mt1
-
-
Generate Application Key:
php artisan key:generate
-
Run Migrations:
php artisan migrate
-
Start the Development Server:
php artisan serve
-
Register a User:
- Use the
/api/register-user
endpoint to create a new user.
curl -X POST http://localhost:8000/api/register-user \ -H "Content-Type: application/json" \ -d '{"email": "john@example.com", "password": "password", "password_confirmation": "password"}'
- Use the
-
Login:
- Use the
/api/login-user
endpoint to authenticate.
curl -X POST http://localhost:8000/api/login-user \ -H "Content-Type: application/json" \ -d '{"email": "john@example.com", "password": "password"}'
- Use the
-
Create a Chat:
- Use the
/api/chat
endpoint to create a new chat with another user.
curl -X POST http://localhost:8000/api/chat \ -H "Authorization: Bearer [token]" \ -H "Content-Type: application/json" \ -d '{"user_id": 2, "is_private": true}'
- Use the
-
Send a Message:
- Use the
/api/chat_message
endpoint to send a message.
curl -X POST http://localhost:8000/api/chat_message \ -H "Authorization: Bearer [token]" \ -H "Content-Type: application/json" \ -d '{"chat_id": 2, "message": "Hello!"}'
- Use the
- Register and log in as a user.
- Create a chat with another user.
- Send and receive messages in real-time.
- Integrate the API with a frontend application for a complete chat experience.
This project includes comprehensive API documentation that details all available endpoints, request/response formats, and authentication methods.
- Full Documentation: See API_DOCUMENTATION.md for complete API reference.
- Authentication: Secure token-based authentication using Laravel Sanctum.
- Real-time Communication: WebSocket integration via Pusher for instant messaging.
- Push Notifications: OneSignal integration for mobile and web notifications.
- RESTful Design: Consistent and intuitive API structure.
- Base URL:
http://localhost/api
(Changes based on deployment environment) - Versioning: All endpoints are prefixed with
/api
-
POST
/api/register-user
: Register a new user and receive authentication token.// Request { "email": "user@example.com", "password": "password", "password_confirmation": "password" } // Response { "data": { "userData": { "username": "user", "email": "user@example.com" }, "token": "1|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" }, "status": "sucess", "message": "User has been register successfully." }
-
POST
/api/login-user
: Authenticate a user and receive token. -
POST
/api/login-user-WithToken
: Authenticate using an existing token. -
POST
/api/logout-user
: Invalidate user's authentication token.
- GET
/api/chat
: List all chats for the authenticated user. - POST
/api/chat
: Create a new chat. - GET
/api/chat/{chat_id}
: Get details of a specific chat.
- GET
/api/chat_message
: Retrieve messages from a specific chat. - POST
/api/chat_message
: Send a message to a specific chat.
- All endpoints (except
/api/register-user
and/api/login-user
) require a Bearer Token for authentication. - Include the token in the
Authorization
header of your API requests:Authorization: Bearer 1|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
200 OK
: Request successful401 Unauthorized
: Authentication required or failed422 Unprocessable Entity
: Validation errors
Refer to the API_DOCUMENTATION.md file for detailed endpoint descriptions, request/response formats, and usage examples.
- Follow PSR-12 coding standards.
- Write clear and concise commit messages.
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Submit a pull request with a detailed description of your changes.
-
Real-Time Messaging Not Working:
- Ensure Pusher credentials are correctly configured in
.env
. - Verify the
BROADCAST_DRIVER
is set topusher
. - Check browser console for WebSocket connection errors.
- Ensure Pusher credentials are correctly configured in
-
Database Connection Errors:
- Double-check your
.env
database credentials. - Ensure MySQL is running.
- Double-check your
-
Push Notifications Not Received:
- Verify OneSignal configuration.
- Check if the user has granted notification permissions.
- Use
php artisan tinker
to interact with your application. - Check Laravel logs in
storage/logs/laravel.log
. - Enable debug mode in
.env
by settingAPP_DEBUG=true
.
This project is licensed under the MIT License. See the LICENSE file for details.
- Maintainer: Mohammed Ilyes Zakarian Bousbaa
- Email: mohammedilyeszakaria.bousbaa@gmail.com
- Support Options: Open an issue on GitHub or contact the maintainer directly.
Thank you for using Chat Application API! We look forward to your contributions and feedback 💙.