A
A
Alexey Berdnikov2015-09-16 09:12:48
comet
Alexey Berdnikov, 2015-09-16 09:12:48

How to send message from server to websocket channel?

I will duplicate the question habrahabr.ru/post/266931/#comment_8574827
How to send a request from the client and notify all other channel subscribers is clear.
How to catch and process on the server what was sent by the client is also clear.
This is what https://github.com/BrainBoxLabs/brain-socket does.
And here's how to make it so that the server sends a message to the channel
. Example: cron gets the results of matches from a remote server once a minute. Registered in the database.
Those who open the page will see the results from the generated page.
How to notify those who did not refresh the page?
On the Koterov realplexor, such a task was solved once or twice, but dragging it and installing it behind you is more problematic than installing the same BrainSocket from the composer and starting the server.
I just hoped that there is the same mechanism as in dklab.ru/lib/dklab_realplexor
-------------------------------- ----------------------------------------
JAVASCRIPT: subscribe to channels, we will listen to them
- -------------------------------------------------- ---------------------

// Create Dklab_Realplexor client.
var realplexor = new Dklab_Realplexor(
"rpl.YourSite.com", // Realplexor's engine URL; must be a sub-domain
"demo_" // namespace
);

// подписка на сообщения отправленные мне лично
realplexor . subscribe(
    "user_1", function (result, id) {
        switch (result . type) {
            case 'private':
                console . log('Вам пришло личное сообщение:');
                console . log('Отправитель: ' + result . sender);
                console . log('Текст: ' + result . text);
                break;
        }
    }
);
//подписка на сообщения для всех
realplexor . subscribe(
    "all", function (result, id) {
        switch (result.type) {
            case 'alert':
                alert(result.message);
                break;
        }
    }
);
// Apply subscriptions. Сallbacks are called asynchronously on data arrival.
realplexor.execute();

-------------------------------------------------- ----------------------
PHP send messages directly to the right channel
--------------------- -------------------------------------------------- -
// Create new API object to access Realplexor server.
require_once "Dklab/Realplexor.php";
$rpl = new Dklab_Realplexor("127.0.0.1", "10010", "demo_");
...
// отправим персональные сообщения пользователю с ID=1 и ID=2
$rpl->send(
    ["user_1", "user_2"], [
        'type'   => 'private',
        'sender' => 'Admin',
        'text'   => 'Привет!',
    ]
);
// отправим всем
$rpl->send(
    ["all"], [
        'type'   => 'alert',
        'message' => 'Окончен матч "Команда1:Команда2", результат матча - 0:2',
    ]
);

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Berdnikov, 2015-10-08
@Groove

Stopped at dklab.ru/lib/dklab_realplexor

A
Alexander Kubintsev, 2015-09-16
@akubintsev

https://github.com/ratchetphp/Pawl - websocket client
But it seems to me that a queue manager would be better suited for this task.
Here is a good example socketo.me/docs/push

L
lyeskin, 2015-09-16
@lyeskin

If I understand the question correctly, then:

$socket = socket_create(AF_UNIX, SOCK_STREAM, 0);
if( socket_connect ( $socket , 'IP сокета', 'порт') ) { //третий аргумент не обязателен
     $msg = 'hello';
     socket_write ( $socket , $msg, strlen($msg) ); //третий аргумент не обязателен
     //вместо socket_write можно использовать socket_send
}

A
Alexander Lozovyuk, 2015-09-16
@aleks_raiden

This is only possible if the websocket is a dedicated server that works all the time

Similar questions

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question