# Install dependencies
npm install
# Copy environment variables
cp .env.example .env
# Start server
npm run dev# Open in browser
http://localhost:3001/index.htmlTest Scenario:
- Select "users" table in left panel
- Select "update" operation
- Click "Send Emit" button
- Listen to "db.users" channel in right panel
- See the event arrive
# Open in browser
http://localhost:3001/monitor.htmlFeature:
- See all connected sockets
- See all connected room via socket users
- See all rooms (need fix)
- See all emits (coming soon)
const io = require('socket.io-client');
// Connect
const socket = io('http://localhost:3001', {
auth: {
token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9...'
}
});
// Listen to all changes in users table
socket.emit('subscribe', 'db.users');
socket.on('db.users', (data) => {
console.log('User change:', data);
});// Listen only to new user insertions
socket.emit('subscribe', 'db.users.insert');
socket.on('db.users.insert', (data) => {
console.log('New user added:', data.record);
});// Listen to all changes for user with ID 123
socket.emit('subscribe', 'db.users.*.123');
socket.on('db.users.*.123', (data) => {
console.log('User 123 updated:', data);
});// Notify database change
socket.emit('dbChange', {
timestamp: new Date().toISOString(),
table: 'products',
action: 'insert',
record: {
id: 456,
name: 'New Product',
price: 99.99
}
});// Notify database change
fetch('http://localhost:3001/api/events', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${adminToken}`
},
body: JSON.stringify({
timestamp: new Date().toISOString(),
table: 'products',
action: 'insert',
record: {
id: 456,
name: 'New Product',
price: 99.99
}
})
});// Listen to product stock changes
socket.emit('subscribe', 'db.products.update');
socket.on('db.products.update', (data) => {
if (data.record.stock < 10) {
alert(`Low stock: ${data.record.name} - ${data.record.stock} units`);
}
});// Listen to new orders
socket.emit('subscribe', 'db.orders.insert');
socket.on('db.orders.insert', (data) => {
updateOrderDashboard(data.record);
playNotificationSound();
});// Listen to all user activities
socket.emit('subscribe', 'db.users');
socket.on('db.users', (data) => {
logUserActivity({
action: data.action,
userId: data.record.id,
timestamp: data.timestamp
});
});const jwt = require('jsonwebtoken');
const userToken = jwt.sign({
sub: 'user_id',
name: 'User Name',
tables: 'users,products,orders',
iat: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000) + (60 * 60) // 1 hour
}, 'your-secret-key');const jwt = require('jsonwebtoken');
// For DB change requests from external systems
const authorizedToken = jwt.sign({
sub: 'external_system_id',
name: 'External System Name',
admin: true,
publisher: false,
iat: Math.floor(Date.now() / 1000),
exp: Math.floor(Date.now() / 1000) + (60 * 60) // 1 hour
}, 'your-secret-key');
// Usage example
const io = require('socket.io-client');
const socket = io('http://localhost:3001', {
auth: { token: authorizedToken }
});
socket.on('connect', () => {
// Send DB change event
socket.emit('dbChange', {
timestamp: new Date().toISOString(),
table: 'users',
action: 'insert',
record: { id: 123, name: 'John Doe' }
});
});// test.js
const io = require('socket.io-client');
const socket = io('http://localhost:3001', {
auth: { token: 'your-token-here' }
});
socket.on('connect', () => {
console.log('✅ Connection successful');
// Send test event
socket.emit('dbChange', {
timestamp: new Date().toISOString(),
table: 'test',
action: 'insert',
record: { id: 1, message: 'Hello World' }
});
});
socket.on('error', (error) => {
console.error('❌ Error:', error);
});# If you get CORS error
# In .env file:
CORS_ORIGIN=*// Test your token
const jwt = require('jsonwebtoken');
try {
const decoded = jwt.verify('your-token', 'your-secret');
console.log('Token valid:', decoded);
} catch (error) {
console.error('Token invalid:', error.message);
}// Run in debug mode
DEBUG=socket.io:* npm run dev
// Check channel subscription
socket.emit('subscribe', 'db.users', (response) => {
console.log('Subscribed channels:', response);
});import { useEffect, useState } from 'react';
import io from 'socket.io-client';
function UserList() {
const [users, setUsers] = useState([]);
useEffect(() => {
const socket = io('http://localhost:3001', {
auth: { token: localStorage.getItem('jwt') }
});
socket.emit('subscribe', 'db.users');
socket.on('db.users', (data) => {
if (data.action === 'insert') {
setUsers(prev => [...prev, data.record]);
}
});
return () => socket.disconnect();
}, []);
return (
<div>
{users.map(user => (
<div key={user.id}>{user.name}</div>
))}
</div>
);
}<template>
<div>
<div v-for="notification in notifications" :key="notification.id">
{{ notification.message }}
</div>
</div>
</template>
<script>
import io from 'socket.io-client';
export default {
data() {
return {
notifications: [],
socket: null
};
},
mounted() {
this.socket = io('http://localhost:3001', {
auth: { token: this.$store.state.jwt }
});
this.socket.emit('subscribe', 'db.notifications');
this.socket.on('db.notifications.insert', (data) => {
this.notifications.push(data.record);
});
},
beforeDestroy() {
if (this.socket) {
this.socket.disconnect();
}
}
};
</script><!DOCTYPE html>
<html>
<head>
<title>Real-time Dashboard</title>
<script src="https://cdn.socket.io/4.7.5/socket.io.min.js"></script>
</head>
<body>
<div id="stats">
<div>Total Users: <span id="userCount">0</span></div>
<div>Active Orders: <span id="orderCount">0</span></div>
</div>
<div id="activity">
<h3>Live Activity</h3>
<ul id="activityList"></ul>
</div>
<script>
const socket = io('http://localhost:3001', {
auth: { token: 'your-jwt-token' }
});
// Listen to user changes
socket.emit('subscribe', 'db.users');
socket.on('db.users', (data) => {
updateUserCount();
addActivity(`User ${data.action}: ${data.record.name}`);
});
// Listen to order changes
socket.emit('subscribe', 'db.orders');
socket.on('db.orders', (data) => {
updateOrderCount();
addActivity(`Order ${data.action}: #${data.record.id}`);
});
function addActivity(message) {
const li = document.createElement('li');
li.textContent = `${new Date().toLocaleTimeString()} - ${message}`;
document.getElementById('activityList').prepend(li);
}
</script>
</body>
</html>- Test Interface: http://localhost:3001/index.html
- Monitoring Interface: http://localhost:3001/monitor.html
- Health Check: http://localhost:3001/api/health
- Socket.io Admin UI: https://admin.socket.io/
- JWT Debugger: https://jwt.io/#debugger-io
- Developer Guide - Detailed technical documentation
- API Examples - Code examples for different platforms
- Environment variables configuration
- SSL/TLS certificate setup
- Load balancer configuration
- Monitoring and logging setup
- Automatic event sending with MySQL triggers
- Microservice architecture integration
- CI/CD pipeline setup
- Horizontal scaling with Redis adapter
- Connection pooling optimization
- Rate limiting fine-tuning
-
Check Documentation
- README.md - General information
- DEVELOPER_GUIDE.md - Technical details
- API_EXAMPLES.md - Code examples
-
Common Issues
- JWT token errors
- CORS issues
- Rate limiting issues
- Connection problems
-
Debug Mode
DEBUG=socket.io* npm run dev -
Log Check
tail -f logs/app.log
- GitHub Issues: Bug reports and feature requests
- Email: For technical support
- Documentation: For updated documentation
🎉 Congratulations! Your MySQL Socket.io Event Server is now running!