You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm using Ratchet and I like its implementation. I'm trying to send a message to all clients connected using chat example class with MessageComponentInterface but I get a null object after executing php script.
chat class:
<?php
use Ratchet\ConnectionInterface;
use Ratchet\MessageComponentInterface;
use Ratchet\Server\IoServer;
require('../vendor/autoload.php');
class ChatServer implements MessageComponentInterface
{
protected $clients;
public function __construct()
{
$this->clients = new \SplObjectStorage;
}
public function onOpen(ConnectionInterface $conn)
{
$this->clients->attach($conn);
$msg = "Connection established!\n";
// send server log to shell
echo $msg;
$this->sendMessageToAll($msg);
}
public function onMessage(ConnectionInterface $from, $msg)
{
foreach ($this->clients as $client) {
// if ($from != $client) {
$client->send($msg);
// }
}
}
# new
public function sendMessageToAll($msg)
{
foreach ($this->clients as $client) {
echo "Sending message to {$client->resourceId} \n";
$client->send($msg);
}
}
# /new
public function onClose(ConnectionInterface $conn)
{
$this->clients->detach($conn);
}
public function onError(ConnectionInterface $conn, Exception $e)
{
$conn->close();
}
}
server.php
require('../vendor/autoload.php');
require('./chat.php');
$app = new Ratchet\App('localhost', 8080);
$app->route('/chat', new ChatServer, array('*'));
$app->route('/echo', new Ratchet\Server\EchoServer, array('*'));
$app->run();
notify.php
require('./chat.php');
$message = "sending message from registerEvent";
echo $message;
$conn = new ChatServer();
$res = $conn->sendMessageToAll($message);
echo '<br/>';
echo json_encode($res);
$res is null when I execute notify.php. I'm testing it separately because my goal is to send a message to all users connected when a new register is created in a database table.
Can you tell me how can I fix it to make it works properly according to my goal?
The text was updated successfully, but these errors were encountered:
$res is null because you are not returning anything from your sendMessageToAll method.
You are approaching this incorrectly. You shouldn't create a server every time you try to send a message. You must run a script with a server in a separate terminal before you run your web app, so that clients can connect to it. And there should be only one instance of the server.
If you want to notify clients about certain events, you can create a special client that will connect to your server and send the notification to other clients, Or, more professional approach, you can implement an event-based architecture in both your server and your web app, using some AMQP or pub/sub implementation (for example, RabbitMQ or Redis).
I'm using Ratchet and I like its implementation. I'm trying to send a message to all clients connected using chat example class with
MessageComponentInterface
but I get a null object after executing php script.chat class:
server.php
notify.php
$res
isnull
when I executenotify.php
. I'm testing it separately because my goal is to send a message to all users connected when a new register is created in a database table.Can you tell me how can I fix it to make it works properly according to my goal?
The text was updated successfully, but these errors were encountered: