Skip to content

Pusher documentation for version 3 #450

@krajcikondra

Description

@krajcikondra

Hi,

I want to send message to my websocket Topic from controller. I found documentation for version 2 but for version 3 is missing.

Have I use symfony/messenger for version 3 instead pushers?

Is there some example of using? I am newbiew in symfony and websockets and some example will help me.

Thanks

This is my ChatTopic where I need implement ReceiiverInterface

<?php

namespace App\Websocket\Topic;

use Gos\Bundle\WebSocketBundle\Client\ClientManipulatorInterface;
use Gos\Bundle\WebSocketBundle\Router\WampRequest;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\Topic;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\Transport\Receiver\ReceiverInterface;
use Symfony\Component\Serializer\Serializer;

final class ChatTopic extends BaseTopic implements ReceiverInterface
{

	private Serializer $serializer;

	public function __construct(
		ClientManipulatorInterface $clientManipulator,
		Serializer $serializer
	) {
		parent::__construct($clientManipulator);
		$this->serializer = $serializer;
	}

	public function onSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request): void
	{
		$topic->broadcast([
			'msg' => $connection->resourceId.' has joined '.$topic->getId(),
			'msgType' => 'subscribeUser',
		]);
	}

	public function onUnSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request): void
	{
		$topic->broadcast([
			'msg' => $connection->resourceId.' has left '.$topic->getId(),
			'msgType' => 'unsubscribeUser',
		]);
	}

	public function onPublish(
		ConnectionInterface $connection,
		Topic $topic,
		WampRequest $request,
        $event,
		array $exclude,
		array $eligible
	): void {
		$message = $this->getTextMessage($event);
		$receiverSessionId = $this->getSessionId($topic, (string) $event['receiverIdentifier']); // @phpstan-ignore-line
		$this->logger->log('info', 'sessionId = ' . $connection->WAMP->sessionId); // @phpstan-ignore-line

		$topic->broadcast([
			'msg' => $message,
			'msgType' => 'userMessage',
		],
			[],
			[$receiverSessionId]
		);
	}

	public function getName(): string
	{
		return 'chat.topic';
	}

	public function get(): iterable
	{
		// here I need $topic and broadcast message from envelope to websocket clients
	}

	public function ack(Envelope $envelope): void
	{
		// TODO: Implement ack() method.
	}

	public function reject(Envelope $envelope): void
	{
		// TODO: Implement reject() method.
	}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions