'invalid'); if(!is_array($connection)) $connection = array(); $type = (string)($message['type'] ?? ''); $room = (string)first($message['room'] ?? false, $connection['room'] ?? false, 'starter-demo'); $commands = array(); if($type === 'session-connect') { $commands[] = array('type' => 'session', 'data' => array( 'room' => $room, 'app' => 'web-app-starter', )); $commands[] = array('type' => 'send', 'message' => array( 'type' => 'realtime.welcome', 'text' => 'Realtime connection established.', 'room' => $room, 'time' => date(DATE_ATOM), )); } else if($type === 'client-ping') { $commands[] = array('type' => 'send', 'message' => array( 'type' => 'realtime.pong', 'text' => 'Pong from PHP backend.', 'time' => date(DATE_ATOM), )); } else if($type === 'client-demo-broadcast') { $text = trim((string)($message['text'] ?? '')); if($text === '') $text = 'Demo broadcast'; $commands[] = array('type' => 'send', 'match' => array('room' => $room), 'message' => array( 'type' => 'realtime.broadcast', 'text' => $text, 'room' => $room, 'time' => date(DATE_ATOM), )); } else if($type === 'session-disconnect') { // No command required. This hook is where an app would update presence. } else { $commands[] = array('type' => 'send', 'message' => array( 'type' => 'realtime.error', 'error' => 'unknown_message_type', 'received_type' => $type, )); } print(json_encode($commands, JSON_UNESCAPED_SLASHES));