A stunning WhatsApp-like real-time chat application built with Laravel and Socket.IO
π Quick Start β’ π± Features β’ π Installation β’ π» Usage β’ π§ Troubleshooting
This is a real-time chat application that brings the power of modern messaging to your Laravel application! Think WhatsApp, but built with cutting-edge web technologies. It features instant messaging, online status tracking, typing indicators, and message delivery receipts - all wrapped in a beautiful, responsive interface.
- Learning real-time web development
- Building chat features for your apps
- Understanding WebSocket communication
- Creating modern messaging interfaces
- β‘ Instant messaging - Messages appear instantly
- π Typing indicators - See when someone is typing
- π’ Online status - Know who's online/offline
- β Message receipts - Track delivery and read status
- π Auto-reconnection - Never lose connection
- π¨ Beautiful UI - Clean, modern design
- π Message bubbles - Sent/received message styling
- π± Mobile responsive - Works on all devices
- β° Timestamps - See when messages were sent
- π’ Unread counts - Badge notifications
- π‘οΈ Laravel Sanctum - Secure API authentication
- π Token-based auth - Modern authentication
- ποΈ MySQL database - Reliable data storage
- β‘ Optimized queries - Fast performance
Backend | Real-time | Frontend | Database |
|
|
|
|
# 1οΈβ£ Clone and setup
git clone <your-repo-url>
cd laravel-socket.io
# 2οΈβ£ Install everything
composer install && npm install
# 3οΈβ£ Configure environment
cp .env.example .env
php artisan key:generate
# 4οΈβ£ Setup database (create MySQL database first)
php artisan migrate
# 5οΈβ£ Start both servers (2 terminals)
# Terminal 1:
php artisan serve --host=127.0.0.1 --port=8080
# Terminal 2:
node socket.js
# 6οΈβ£ Open browser
# Go to: http://127.0.0.1:8080
π That's it! You're ready to chat!
Before we begin, make sure you have these installed:
Software | Version | Download Link |
---|---|---|
π PHP | 8.2+ | php.net |
π¦ Composer | Latest | getcomposer.org |
π’ Node.js | 16+ | nodejs.org |
ποΈ MySQL | 5.7+ | mysql.com |
π§ Git | Latest | git-scm.com |
- RAM: 2GB minimum (4GB recommended)
- Storage: 1GB free space
- OS: Windows 10+, macOS 10.14+, or Linux
# Clone the project
git clone <your-repository-url>
cd laravel-socket.io
# Or if you already have it locally
cd C:\Users\Subhash\Documents\GitHub\laravel-socket.io
# Install PHP packages
composer install
# Install Node.js packages
npm install
What this installs:
- π Laravel Framework & Sanctum
- β‘ Socket.IO & Express.js
- π¨ Bootstrap & jQuery
- π§ Build tools (Vite)
# Copy environment file
cp .env.example .env
# Generate application key
php artisan key:generate
Edit your .env
file:
APP_NAME="Laravel Socket Chat"
APP_ENV=local
APP_DEBUG=true
APP_URL=http://127.0.0.1:8080
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_socket_chat
DB_USERNAME=your_username
DB_PASSWORD=your_password
Create MySQL Database:
CREATE DATABASE laravel_socket_chat;
Run Migrations:
php artisan migrate
This creates:
- π₯
users
table (user accounts) - π¬
messages
table (chat messages) - π
personal_access_tokens
table (API auth) - π
sessions
table (user sessions)
You need TWO terminals running:
php artisan serve --host=127.0.0.1 --port=8080
Expected output:
Starting Laravel development server: http://127.0.0.1:8080
node socket.js
Expected output:
Socket.IO server running on port 3000
- Open your browser
- Go to:
http://127.0.0.1:8080
- Register a new account or Login
- Click "Chat" to start messaging!
- π Visit the home page
- π Click "Register" to create account
- π Or "Login" if you have an account
- π¬ Click "Chat" in navigation
- π₯ See list of all users
- π±οΈ Click any user to start conversation
- βοΈ Type your message
- β Press Enter or click Send
- β‘ Message appears instantly!
Icon | Status | Meaning |
---|---|---|
β° | Sending | Message being sent to server |
β | Delivered | Message reached recipient's device |
β β | Read | Message has been read by recipient |
- Green dot = User is online
- Gray dot = User is offline
- Real-time updates = Status changes instantly
- See when someone is typing
- Auto-hide after 1 second of inactivity
- Real-time updates via Socket.IO
- Responsive design works on all devices
- Touch-friendly interface
- Collapsible sidebar for mobile
- All messages stored in database
- Previous conversations load automatically
- Messages persist between sessions
- Red badges show unread count
- Counts reset when opening conversation
- Real-time updates for new messages
- See last message in each conversation
- Timestamps show when sent
- Updates in real-time
All API endpoints require authentication via Laravel Sanctum token.
Headers Required:
Authorization: Bearer {your_api_token}
Content-Type: application/json
POST /api/send
Request:
{
"to": 2,
"message": "Hello, how are you?"
}
Response:
{
"success": true,
"id": 123
}
GET /api/pending
Response:
[
{
"id": 123,
"from": 2,
"from_name": "John Doe",
"message": "Hello there!",
"sent_at": "2024-01-15T10:30:00.000000Z"
}
]
GET /api/messages?with=2
Response:
[
{
"id": 123,
"from": 1,
"from_name": "Jane Smith",
"message": "Hello!",
"sent_at": "2024-01-15T10:30:00.000000Z",
"is_me": true
}
]
POST /api/mark-delivered/123
POST /api/mark-read/123
GET /api/last-message/2
// Join user room
socket.emit('join', userId);
// Typing indicator
socket.emit('typing', {
user_id: userId,
user_name: userName,
to: recipientId
});
// Stop typing
socket.emit('stop-typing', {
user_id: userId,
to: recipientId
});
// Message read
socket.emit('message-read', {
message_id: messageId,
from: senderId,
to: recipientId
});
// User came online
socket.on('online', (data) => {
console.log('User online:', data.user_id);
});
// New message received
socket.on('new-message', (data) => {
console.log('New message:', data);
});
// Someone is typing
socket.on('typing', (data) => {
console.log('User typing:', data.user_name);
});
// Message was read
socket.on('message-read', (data) => {
console.log('Message read:', data.message_id);
});
CREATE TABLE users (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
api_token VARCHAR(80) UNIQUE NULL,
created_at TIMESTAMP NULL,
updated_at TIMESTAMP NULL
);
CREATE TABLE messages (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
from_id BIGINT NOT NULL,
to_id BIGINT NOT NULL,
message TEXT NOT NULL,
delivered BOOLEAN DEFAULT 0,
read_at TIMESTAMP NULL,
created_at TIMESTAMP NULL,
updated_at TIMESTAMP NULL,
FOREIGN KEY (from_id) REFERENCES users(id),
FOREIGN KEY (to_id) REFERENCES users(id)
);
Problem: Browser shows connection errors Solutions:
- β Ensure Socket.IO server is running on port 3000
- β Check firewall settings
- β Verify CORS configuration
- β Check browser console for errors
Problem: php artisan serve
fails
Solutions:
- β Check if port 8080 is in use
- β
Try different port:
php artisan serve --port=8081
- β
Verify PHP version:
php --version
- β
Check Laravel:
php artisan --version
Problem: Migration fails Solutions:
- β
Verify database credentials in
.env
- β Ensure MySQL service is running
- β Check database exists
- β Verify user permissions
Problem: Messages don't reach recipients Solutions:
- β Check Socket.IO server logs
- β Verify API token is valid
- β Check network connectivity
- β Review browser console
Problem: Can't login or API calls fail Solutions:
- β Clear browser cache and cookies
- β Check session configuration
- β Verify Sanctum setup
- β Regenerate API tokens
Enable detailed error information:
# In .env file
APP_DEBUG=true
LOG_LEVEL=debug
Check logs:
# Laravel logs
tail -f storage/logs/laravel.log
# Socket.IO logs (in terminal where node socket.js is running)
APP_ENV=production
APP_DEBUG=false
APP_URL=https://yourdomain.com
# Use Redis for sessions in production
SESSION_DRIVER=redis
CACHE_DRIVER=redis
- π PHP 8.2+ with required extensions
- ποΈ MySQL 5.7+ or MariaDB 10.3+
- π’ Node.js 16+ and NPM
- π Web Server: Nginx or Apache
- βοΈ Process Manager: PM2 or Supervisor
# Install production dependencies
composer install --optimize-autoloader --no-dev
npm install --production
# Generate optimized assets
npm run build
php artisan config:cache
php artisan route:cache
php artisan view:cache
# Run migrations
php artisan migrate --force
# Start services with PM2
pm2 start socket.js --name "socket-server"
pm2 start "php artisan serve --host=0.0.0.0 --port=8000" --name "laravel-server"
- Modify Bootstrap classes in templates
- Add custom CSS in
resources/css
- Update JavaScript for UI interactions
- Test on different devices
- Create new API endpoints in
MessageController
- Add new Socket.IO events in
socket.js
- Update frontend JavaScript in
chat.blade.php
- Add database migrations for new tables/columns
- Socket.IO port: Modify
socket.js
- Laravel port: Change in
php artisan serve
- Database: Update
.env
file - CORS: Configure in
socket.js
- π΄ Fork the repository
- πΏ Create feature branch:
git checkout -b feature/amazing-feature
- πΎ Commit changes:
git commit -m 'Add amazing feature'
- π€ Push to branch:
git push origin feature/amazing-feature
- π Open Pull Request
- Follow PSR-12 coding standards
- Write meaningful commit messages
- Add tests for new features
- Update documentation
- Laracasts - Video tutorials
- Laravel Daily - Tips and tutorials
- Socket.IO Examples
This project is open-sourced software licensed under the MIT license.
Having issues? We're here to help!
- π Check the troubleshooting section above
- π Search existing issues in the repository
- π Create a new issue with detailed information
- π¬ Join the community for help and discussions
Congratulations! You now have a complete real-time chat application! π
What you've built:
- β Real-time messaging with Socket.IO
- β Beautiful WhatsApp-like interface
- β Message status tracking
- β Online/offline status
- β Typing indicators
- β Mobile-responsive design
- β Secure authentication
Next steps:
- π Deploy to production
- π¨ Customize the design
- β‘ Add new features
- π± Test on different devices
Happy coding! ππ»
Made with β€οΈ using Laravel & Socket.IO
β Star this repo β’ π Report Bug β’ π‘ Request Feature