Laravel App Connect and Emit to Socket.io
Laravel's built-in broadcasting system works well with Pusher, but Pusher is a paid service. If you are running your own Node.js Socket.io server, you can have your Laravel backend connect directly to it and emit events without relying on any third-party paid service.
This guide covers connecting a Laravel controller to a Socket.io server using a PHP Socket.io client library.
The Setup
The architecture is straightforward:
- A Node.js server runs Socket.io and manages connected clients (Angular apps, Android apps, browsers)
- A Laravel form submission triggers a controller action
- The controller connects to the Socket.io server and emits an event
- The Socket.io server broadcasts the event to all connected clients in real time
Step 1: Add a PHP Socket.io Client Library
Create a socket folder inside your app's app directory:
mkdir app/socket
Add a PHP Socket.io client to this folder. One option is the psinetron/PHP_SocketIO_Client library available on GitHub. Download the SocketIO.php file and place it in app/socket/SocketIO.php.
Alternatively, if you prefer Composer, there are several PHP Socket.io client packages available — search Packagist for socket.io client and choose one that fits your Socket.io server version.
Step 2: Emit from a Laravel Controller
In your controller, import and use the Socket.io client:
use App\socket\SocketIO;
class OrderController extends Controller
{
public function store(Request $request)
{
// Handle form logic...
$socketIO = new SocketIO();
$result = $socketIO->send(
'your-socketio-server-url', // e.g. 'http://localhost'
3000, // port
'order_placed', // event name
json_encode([
'order_id' => $order->id,
'customer' => $order->customer_name,
'total' => $order->total,
])
);
return redirect()->back()->with('success', 'Order placed.');
}
}
Step 3: Listen on the Socket.io Server
On the Node.js side, listen for the event your Laravel controller emits:
const io = require('socket.io')(server);
io.on('connection', (socket) => {
socket.on('order_placed', (data) => {
// Broadcast to all connected clients
io.emit('new_order', JSON.parse(data));
});
});
Step 4: Receive in the Client
In your Angular or JavaScript frontend:
const socket = io('http://your-socketio-server:3000');
socket.on('new_order', (data) => {
console.log('New order received:', data);
// Update the UI
});
Notes
- The PHP Socket.io client opens a short-lived connection to emit the event and then closes. It is not a persistent connection.
- If the Socket.io server is not reachable, the emit will fail. Wrap the send call in a try/catch and consider queuing the emit using Laravel's queue system for reliability.
- For production, ensure the Socket.io server uses HTTPS/WSS and update the URL in the controller accordingly.
Conclusion
Integrating a Laravel backend with a Socket.io server requires only a minimal PHP client library. This approach avoids Pusher costs and keeps real-time event delivery under your control. The pattern scales well: the Socket.io server handles the persistent client connections, while Laravel simply fires events when business logic dictates.