Push notifications from server to user with PHP/JavaScript

Push notifications from server to user with PHP/JavaScript

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

Stefan Bogdanescu

Founder & Senior Architect

Pushing Data to Mobile Clients Without HTTP Polling

Achieving real-time updates from a server to a logged-in user without triggering an HTTP request is a common requirement for mobile and web applications. The goal is to notify the client of new data—such as incoming chat messages or status changes—without forcing the user's device to poll the server at fixed intervals, which wastes battery life and consumes excessive data.

The fundamental answer lies in establishing a persistent, bidirectional connection between the client and the server. Unlike standard HTTP requests which are request-response cycles, this approach requires a long-lived socket connection that stays open as long as the user session is active.

WebSocket Protocol for Real-Time Communication

The industry-standard solution for this scenario is the WebSocket protocol. While you can achieve similar results with Server-Sent Events (SSE) or long-polling, WebSockets are generally preferred for bidirectional communication, such as chat applications where messages flow from sender to receiver instantly.

To implement this in a PHP environment running on Apache, you have two primary architectural paths. The first is to use the php-websocket library or similar extensions that allow PHP to handle WebSocket handshakes and framing directly. This keeps your logic entirely within your existing PHP stack but requires careful attention to resource management, as keeping thousands of persistent connections open in PHP can consume significant memory if not handled via an event-driven architecture like ReactPHP or Swoole.

The second, and often more robust path for production environments, is offloading the WebSocket handling to a dedicated service like Node.js (using ws or Socket.io) or Go, which your PHP backend can communicate with via HTTP or direct IPC. However, if you must stick to pure PHP, libraries that wrap the underlying socket extensions are viable for moderate traffic loads.

Client-Side Integration and Session Binding

On the client side, whether running in a mobile browser or a PhoneGap application, the implementation begins by opening a WebSocket connection upon user authentication. You cannot push data without first establishing this channel.

// Establish the connection after successful login
const socket = new WebSocket('wss://yourdomain.com/chat');

socket.onopen = () => {
    // Associate this connection with the current session ID
    // The server must validate the session during the handshake or via an initial ping/pong
};

socket.onmessage = (event) => {
    const data = JSON.parse(event.data);
    
    if (data.type === 'new_message') {
        // Update UI immediately without triggering a new HTTP request
        renderNewMessage(data.content, data.sender);
    }
};

Once connected, the server can broadcast messages to specific sessions. When a user submits a chat message, your PHP backend processes the input and then iterates through its list of active WebSocket connections. It filters these connections based on the recipient's session ID (often stored as a header or passed during the handshake) and pushes the data down the socket pipe.

Trade-offs and Operational Considerations

While eliminating polling sounds ideal, it introduces specific operational complexities. The primary challenge is connection lifecycle management. Mobile networks are notoriously unstable; a dropped cellular connection severs the WebSocket link. Your server must handle these disconnections gracefully by tracking which sessions are active and avoiding attempts to push data to dead sockets.

Furthermore, security cannot be an afterthought. Since WebSockets maintain a persistent open channel, they are prime targets for hijacking if not properly secured. You must enforce TLS (wss://) to prevent Man-in-the-Middle attacks. Additionally, the server must validate the session token during the initial handshake to ensure that only authenticated users can receive private data streams.

Another critical consideration is scalability. A monolithic PHP application handling thousands of concurrent WebSocket connections may hit memory limits or CPU saturation. In high-traffic scenarios, it is often better to decouple the WebSocket layer from your main PHP logic, perhaps by running a separate Node.js service that acts as the WebSocket gateway while your PHP app focuses on business logic and database persistence.

Finally, consider the fallback strategy. If a user switches from Wi-Fi to 3G or loses connectivity entirely, the client should automatically attempt to reconnect. Libraries like Socket.io abstract this complexity away by providing automatic reconnection and message buffering, ensuring that once the connection is restored, the user receives any messages sent during the outage without needing to manually refresh the page.

Our network