public static function encode_websocket_events($events)
    {
        $out = '';
        foreach ($events as $event) {
            if (!is_null($event->content)) {
                $content_length = dechex(strlen($event->content));
                $out .= "{$event->type} {$content_length}\r\n" .
                        "{$event->content}\r\n";
            } else {
                $out .= "{$event->type}\r\n";
            }
        }
        return $out;
    }