JDare/ClankBundle

Broadcast from eventlistener to topic

npinon opened this issue · 2 comments

Hi,

Is it possible to broadcast something to a topic from a custom Event Listener Class. I can't find the way to do it :/

Exemple : I want to broadcast a message when a client is disconnected.

Thx

You just have to create your topic class, and implement onUnSubscribe:

    /**
     * This will receive any UnSubscription requests for this topic.
     *
     * @param \Ratchet\ConnectionInterface $conn
     * @param $topic
     * @return void
     */
    public function onUnSubscribe(Conn $conn, $topic)
    {
        //this will broadcast the message to ALL subscribers of this topic.
        $topic->broadcast($conn->resourceId . " has left " . $topic->getId());
    }

See documentation: https://github.com/JDare/ClankBundle/blob/master/Resources/docs/TopicSetup.md

Edit: Ok but that is not triggered when an user leave the page. I am searching for a solution, the best should be to auto unsuscribe to all topic when user disconnect.

For now you still can unsuscribe from javascript when user leave page, by using onbeforeunload event:

window.onbeforeunload = function() {
    clankSession.unsubscribe('chat/my/topic');
};

It (solution with window.onbeforeunload) works! Thank you 😃
It's not pretty clean code but it's better than nothing