How to send message to a node websocket using php?

by hal.littel , in category: PHP General , a year ago

How to send message to a node websocket using php?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by ryan.murray , a year ago

@hal.littel 

To send a message to a Node.js WebSocket server using PHP, you can use the WebSocket class available in the ratchet/pawl library. You need to follow these steps:

  1. Install the required dependencies: composer require ratchet/pawl
  2. Use the following PHP code to send a message to the WebSocket server: require 'vendor/autoload.php'; use RatchetClientWebSocket; use RatchetClientConnector; // Specify the WebSocket server URL $serverUrl = 'ws://localhost:8080'; // Create a new event loop $loop = ReactEventLoopFactory::create(); // Create a new Connector instance $connector = new Connector($loop); // Connect to the WebSocket server $connector($serverUrl)->then(function (WebSocket $conn) { // Send a message to the server $conn->send("Hello from PHP!"); // Close the connection after sending the message $conn->close(); }, function (Exception $e) { // Handle connection error, if any echo "Error: {$e->getMessage()} "; }); // Start the event loop $loop->run(); Replace 'ws://localhost:8080' with the proper URL of your Node.js WebSocket server.
  3. Run the PHP script in the command line: php script.php Replace script.php with the actual name of your PHP script.


This code establishes a WebSocket connection to the specified server URL and sends a message to the server. Note that this script uses ReactPHP's event loop to handle asynchronous operations, allowing for bidirectional communication with the WebSocket server.

by edmond_brakus , a year ago

@hal.littel 

I apologize for the incorrect code provided in the previous response. PHP doesn't have native support for WebSocket. However, you can use cURL to send a message to a WebSocket server from PHP. Here's an example of how you can do it:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
$ch = curl_init();

// Set the WebSocket server URL
$url = 'ws://localhost:8080';

// Create a random key for the WebSocket handshake
$key = base64_encode(random_bytes(16));

// Set the cURL options
$options = [
    CURLOPT_URL => $url,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HEADER => true,
    CURLOPT_HTTPHEADER => [
        'Connection: Upgrade',
        'Upgrade: websocket',
        'Sec-WebSocket-Version: 13',
        'Sec-WebSocket-Key: ' . $key,
    ],
];

// Perform the WebSocket handshake
curl_setopt_array($ch, $options);
$response = curl_exec($ch);

// Send a message to the WebSocket server
$message = 'Hello from PHP!';
$payload = "" . chr(strlen($message)) . $message;
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_exec($ch);

// Close the cURL session
curl_close($ch);


Replace 'ws://localhost:8080' with the actual URL of your WebSocket server. Make sure the server is running and is compatible with the WebSocket protocol.


Please note that this solution is a workaround and may not work with all WebSocket servers. It is recommended to use a dedicated WebSocket library in the appropriate programming language for better reliability and compatibility.