How to integrate nodeJS + Socket.IO and PHP?

How to integrate nodeJS + Socket.IO and PHP?

  1. Blog
  2. 15 November 2026
  3. 4 min read

Stefan Bogdanescu

Founder & Senior Architect

Bridging Node.js and PHP with Socket.IO

Developing a full-stack application entirely in Node.js isn't always the best fit. Sometimes you need the robust ecosystem of existing PHP frameworks like Symfony or CodeIgniter for your core business logic, while leveraging Node.js specifically for high-concurrency tasks like real-time notifications or chat features. The challenge arises when trying to connect these two worlds without introducing architectural complexity like a third server or relying solely on cURL round-trips.

The goal is often to handle the heavy lifting in PHP while using simple JavaScript on the client side to catch events emitted from the Node.js module. This requires a specific setup where the Socket.IO server runs independently of the PHP application, yet remains transparent to the frontend.

The Architecture: Two Servers, One Socket.IO Instance

To achieve communication between Node.js and PHP without an intermediate proxy or message queue, you must run Socket.IO as a standalone service. In this pattern, the Node.js process acts purely as the WebSocket server, handling the raw TCP connection upgrade and managing the socket events. The PHP application (running on Apache, Nginx with PHP-FPM, or CLI) does not need to be aware of the protocol details; it simply listens for signals triggered by the Node server.

This separation ensures that the client-side JavaScript remains consistent. Whether the user is interacting with a feature managed by PHP or one managed by Node, they connect to ws://yourdomain.com/socket.io. The routing logic happens internally, but from the developer's perspective, the protocol is unified.

Implementing the Node.js Server

On the Node side, you need an instance of socket.io that listens on a specific port or path. You can install this via npm:

npm install socket.io

Create a simple file, say server.js, to initialize the server. This script should run independently, either as a standalone process or behind a reverse proxy like Nginx configured to route /socket.io traffic to the Node process.

const io = require('socket.io')(3001);

io.on('connection', (socket) => {
    console.log('New client connected:', socket.id);

    // Example: Listen for events PHP emits
    socket.on('php-event-trigger', (data) => {
        console.log(`Received event from PHP: ${data}`);
        
        // Emit a real-time update to the client
        io.emit('realtime-notification', {
            message: 'New notification received!',
            payload: data
        });
    });

    socket.on('disconnect', () => {
        console.log('Client disconnected');
    });
});

In this setup, the Node server waits for events. It does not initiate connections to PHP directly; instead, it reacts to triggers that PHP sends via standard HTTP requests or signals once a connection is established in the underlying transport layer (often HTTP long-polling fallback if WebSockets aren't supported by the browser).

Triggering Events from PHP

The trickiest part of this integration is getting PHP to "talk" to the Node server without opening a raw TCP port directly between them (which can be tricky with Apache) or using cURL in a way that breaks the real-time illusion. The standard approach involves using an HTTP request to trigger the event on the Node side, assuming the Node process is listening for these triggers.

You can use PHP's built-in cURL library to send a simple POST request to a designated endpoint on your Node server. For example, if you expose a /trigger route in your Node app (or handle it via the same Socket.IO transport mechanism depending on your proxy setup), PHP can push data.

<?php
$url = 'http://localhost:3001/trigger'; // Or your specific endpoint

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['type' => 'user_action']));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);
curl_close($ch);
?>

However, a more seamless integration often involves setting up the Node server to listen on a standard HTTP port (like 3001) and having Apache proxy specific paths to it. This allows you to keep the Socket.IO library handling the WebSocket upgrades while using standard HTTP for event triggers if needed, or letting the same port handle both depending on your proxy configuration.

Client-Side Integration

Once the infrastructure is in place, the client-side code remains agnostic of the backend stack. You simply connect to the domain and subscribe to the events you care about.

const socket = io('http://localhost');

socket.on('connect', () => {
    console.log('Connected to the real-time server');
});

// Listen for notifications coming from Node.js
socket.on('realtime-notification', (data) => {
    console.log('Notification:', data.message);
    // Update UI here
});

Trade-offs and Considerations

The primary trade-off here is operational complexity. You are now managing two distinct server processes: one for your PHP application and one for your Node.js Socket.IO instance. This means you need to handle deployments, scaling, and security policies (like firewalls) for both. If you run on a shared hosting environment where you cannot start arbitrary Node processes, this pattern becomes significantly harder to implement.

Another consideration is the routing of HTTP vs WebSocket traffic. If your PHP app handles all other routes, you must ensure your reverse proxy (Nginx/Apache) correctly routes /socket.io requests to the Node process while leaving /api/* or other paths for PHP. Misconfiguration here will result in "Cannot GET /socket.io" errors.

Finally, keep in mind that cURL usage between the two servers is synchronous and blocking within the PHP request context. If you are trying to trigger events asynchronously without waiting for a response, ensure your Node server doesn't block the connection or that your PHP script doesn't hang waiting for a reply it never expects.

Our network