Get a Pentest and security assessment of your IT network.

Cyber Security

Firewall-Friendly Data Push

TL;DR

Pushing data to a client behind a firewall is tricky. WebSockets are often the best solution, but if those aren’t possible, consider long polling or Server-Sent Events (SSE). If even those are blocked, you might need to rely on the client initiating requests regularly (polling) – less efficient, but reliable.

Understanding the Problem

Firewalls block unsolicited incoming connections. Your client is likely behind a firewall that prevents direct pushes from your server. The goal is to find a method where the connection originates from the client, or uses ports/protocols the firewall allows.

Solution Steps

  1. Check Existing Ports and Protocols: Before anything else, confirm what ports are open on the client’s firewall. Common ones include:
    • 80 (HTTP): Usually open for web browsing.
    • 443 (HTTPS): Usually open for secure web browsing.
  2. WebSockets (Recommended): WebSockets provide a persistent, two-way communication channel over HTTP(S).
    • The initial connection is made via HTTP, so it usually gets through firewalls.
    • Once established, data can flow both ways in real-time.
    • Implementation: You’ll need a WebSocket server (e.g., using Node.js with the ws package, Python with websockets). The client connects to this server.
      // Example Node.js WebSocket Server
      const WebSocket = require('ws');
      
      const wss = new WebSocket.Server({ port: 8080 });
      
      wss.on('connection', ws => {
        console.log('Client connected');
        ws.send('Welcome to the server!');
      });
      
  3. Long Polling: If WebSockets are blocked, long polling is a good alternative.
    • The client makes an HTTP request to your server.
    • Your server holds the connection open until it has new data for the client.
    • Once data is available, the server sends a response.
    • The client immediately re-establishes the connection.
      // Example (simplified) PHP Long Polling Server
      <?php
        while (true) {
          if ($newDataAvailable) {
            header('Content-Type: text/plain');
            echo $newData;
            break;
          } else {
            sleep(1);
          }
        }
      ?>
  4. Server-Sent Events (SSE): SSE allows the server to push updates to the client over HTTP.
    • The client makes an HTTP request.
    • Your server sends a stream of data updates until the connection is closed.
      // Example Node.js Server-Sent Events
      const http = require('http');
      
      const server = http.createServer((req, res) => {
        res.setHeader('Content-Type', 'text/event-stream');
        res.setHeader('Cache-Control', 'no-cache');
      
        setInterval(() => {
          res.write('data: Hello from the server!nn');
        }, 1000);
      });
      
      server.listen(8080);
      
  5. Regular Polling (Last Resort): If all else fails, the client must periodically request data from your server.
    • The client makes HTTP requests at fixed intervals.
    • This is less efficient than WebSockets, long polling or SSE because of overhead and potential delays.
    • Implementation: Use JavaScript’s setInterval to make regular AJAX calls.
      // Example JavaScript Polling
      setInterval(() => {
        fetch('/data')
          .then(response => response.json())
          .then(data => console.log(data));
      }, 5000); // Poll every 5 seconds
      
  6. Consider a Message Queue: For more complex scenarios, use a message queue (e.g., RabbitMQ, Kafka).
    • The client can subscribe to messages from the queue.
    • This decouples your server from the client and provides greater scalability and reliability.

Important Considerations

  • Security: Always use HTTPS for secure communication, especially when transmitting sensitive data.
  • Firewall Configuration (Client-Side): If possible, work with the client to configure their firewall to allow connections on the necessary ports and protocols.
  • Error Handling: Implement robust error handling in both your server and client code to deal with connection issues and data transmission failures.
Related posts
Cyber Security

Zip Codes & PII: Are They Personal Data?

Cyber Security

Zero-Day Vulnerabilities: User Defence Guide

Cyber Security

Zero Knowledge Voting with Trusted Server

Cyber Security

ZeroNet: 51% Attack Risks & Mitigation