Chat: Controllers


1Message controller

This controller is called using AJAX. It receives two POST parameters:

  • from: the name of the message sender.
  • text: the message text.

The data received is sent to the ZeroMQ PUSH socket (which is connected to the broker). The value it returns to the browser is always true, serialized in JSON.

Here are the contents of the controller/Message.php file:

<?php

use \Temma\Attributes\View as TµView;

/** Message controller. */
#[TµView('~Json')]
class Message extends \Temma\Web\Controller {
    /** Action that receives new messages. */
    public function broadcast() {
        // set return value
        $this['json'] = true;
        // retrieve POST parameters
        $from = $_POST['from'] ?? null;
        $text = $_POST['text'] ?? null;
        // check data
        if (!trim($from) || !trim($text))
            return;
        // send data to the broker
        $this->zmq_client_push[''] = [
            'from' => $from,
            'text' => $text,
        ];
    }
}
  • Line 3: An alias is defined for the attribute used to define the view to be used for a controller or action.
  • Line 6: Use the attribute to define that the Message controller always uses the Json view.
  • Line 7: Creation of the Homepage controller, which is a classic controller (it inherits from \Temma\Web\Controller).
  • Line 9: Definition of the broadcast() action.
  • Line 11: Defines that the action will always return the boolean value true.
  • Lines 13 and 14: Retrieve POST data.
  • Lines 16 and 17: Check that data received is not empty (after removing leading and trailing spaces).
  • Lines 19 to 22: Data is sent to the ZeroMQ PUSH socket.

2Event controller

The browser establishes a connection with this controller, which opens an SSE connection. These connections are a little unusual in that they remain open so that the server can send events to the browser whenever it wishes. If the connection is broken, the browser will automatically reopen it.

The Event controller opens a ZeroMQ SUB socket, which is connected to the broker's PUB socket. A PUB-SUB connection means that every time the broker sends data, all running Event controllers will receive it.

The controller therefore runs indefinitely, in read-only mode on its socket. When it receives data, it simply sends it to the browser via SSE.

Here are the contents of the controller/Event.php file:

<?php

/** Event controller. */
class Event extends \Temma\Web\EventController {
    /** Action that sends SSE notifications. */
    public function fetch() {
        // infinite loop
        while (true) {
            // reading a message sent by the broker
            $message = $this->zmq_client_sub[''];
            // sending the message to the customer on the "msg" channel
            $this['msg'] = $message;
        }
    }
}
  • Line 4: Creation of the Event controller, which is an event controller (it inherits from \Temma\Web\EventController).
  • Line 6: Creation of the fetch() action.
  • Lines 8 to 13: Infinite loop, so that processing continues as long as the browser keeps the connection open.
  • Line 10: Reading (blocking) on ZeroMQ SUB socket.
  • Line 12: The message received on the SUB socket is sent to the browser by SSE on the "msg" channel (which is the channel to which the browser is subscribed, see Javascript code).