my problem lies in a single error that I can't solve. In order to develop an instant messaging system, I use a redis server and then a library called predis for php to send events to an SSE instance when a message is sent. Here's my connection code to my local redis server, predis.php:
<?php
require '../predis-2.2.1/autoload.php';
use Predis\Client;
$redis = new Client([
'scheme' => 'tcp',
'host' => '127.0.0.1',
'port' => 6379,
]);
// Checking the connection
// if ($redis->ping() == "PONG") {
// echo "Connection to Redis established.";
// } else {
// echo "Unable to connect to Redis.";
// }
For information, the connection according to my code does not comment works correctly between php and the local redis server Here's my code for sending events to the redis server, send_msg.php :
$newMessage = [];
$newMessage['id'] = $lastInsertId_t;
$newMessage['text'] = $text;
$newMessage['user'] = $id_user;
$newMessage['conv'] = $id_conv;
$newMessage['nat'] = 0;
$newMessage['state'] = 0;
$newMessage['prov'] = $prov;
$channelName = 'sse_' . $id_conv; //sse_5Lrc22xuc5LJ57hv7Y3k6E2DGk7hMzR37K7E237t6NNyS44uUAvYfqD9H8yG2Y6kAUzyau
$messageData = json_encode($newMessage); //{"id":"491","text":"test","user":"U2fA44x5skTBy5z67t3G84Z6CK2yJVtGYmDscAuF69kLuDJ5gTDh7265j89j2kiM43vLGv","conv":"5Lrc22xuc5LJ57hv7Y3k6E2DGk7hMzR37K7E237t6NNyS44uUAvYfqD9H8yG2Y6kAUzyau","nat":0,"state":0,"prov":"0"}
$redis->publish($channelName, $messageData);
Here's my experimental code for receiving events sent to the same redis channel, test.php :
<?php
include 'predis.php';
$channelName = 'sse_5Lrc22xuc5LJ57hv7Y3k6E2DGk7hMzR37K7E237t6NNyS44uUAvYfqD9H8yG2Y6kAUzyau';
$num = 0;
function handleMessage($message) {
$messageData = json_decode($message);
echo "data: " . json_encode($messageData) . "\n\n";
exit;
}
while ($num != 100) {
// Attendre des données depuis le canal
$redis->subscribe([$channelName], 'handleMessage');
$num++;
}
?>
My problem is that when I run test.php, I receive looping errors at each iteration of my while. Here are the errors I receive:
array to string conversion in C:\wamp64\www\predis-2.2.l\src\Connection\StreamConnection.php at the line 368
array to string conversion in C:\wamp64\www\predis-2.2.1\src\Connection\StreamConnection.php at the line 369
So with these errors, I don't receive the event sent when user 1 sends a message to user 2. Could you suggest a solution?