WebSocketBundle icon indicating copy to clipboard operation
WebSocketBundle copied to clipboard

Pusher documentation for version 3

Open krajcikondra opened this issue 4 years ago • 2 comments

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.
	}
}

krajcikondra avatar Oct 25 '21 10:10 krajcikondra

Same problem :(

I tried use older version but is not compatible with symfony 5.

It is possible use deprecated WampPusher class? I haven`t registered WampPusher as service. When I register WampPusher as service I get error that WampPusher need WampRouter which is not registered as service. WampRouter has depencency of type RouterInterface .....

How can I register all necessary services? Am I doing something bad?

Unfortunatelly in docs missing example

ViktorieTrungerova avatar Oct 31 '21 18:10 ViktorieTrungerova

Same problem, here. I opened another issue :

https://github.com/GeniusesOfSymfony/WebSocketBundle/issues/463

I'm posting this here in case someone has an answer.

LucasWerner437 avatar Apr 08 '22 11:04 LucasWerner437