Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message lost on 2 separate connections to same URL #147

Open
bobrundle opened this issue May 5, 2023 · 1 comment
Open

Message lost on 2 separate connections to same URL #147

bobrundle opened this issue May 5, 2023 · 1 comment

Comments

@bobrundle
Copy link

Using ratchet/pawl v0.4.1 and PHP 7.4

Implemented pub/sub client in PHP...

public function publish(string $user, string $group, string $message)
    {
        $hubUrl = $this->getHubUrl();
        $accessToken = $this->getAccessToken($user);
        $url = $hubUrl.'?access_token='.$accessToken;
        $this->user = $user;
        $this->group = $group;
        $this->message = $message;
        \ratchet\client\connect($url,['json.webpubsub.azure.v1'])->then(function($conn) {
            $sendMessage = [
                'type' => 'sendToGroup',
                'group' => $this->group,
                'data' => [
                    'message' => $this->message
                ]
            ];
            $sendMessageText = json_encode($sendMessage);

            $joingroup = [
                "type" => "joinGroup",
                "group" => $this->group
            ];            
            $joingroupText = json_encode($joingroup);
            $conn->send($joingroupText);
            $conn->send($sendMessageText);
            $conn->close();
        }, function ($e) {
            error_log("Could not connect: {$e->getMessage()}\n");
        });        
    }

This works great. However, if I send back-to-back messages, e.g.

    $pubSubService->publish('server','activity-queue',json_encode($queueEntry));
    $pubSubService->publish('server','agent-table',json_encode($agent));

The first message is always lost. I've fixed this by queuing up the messages to the same URL and sending them together on the same connection. A much better design, I admit.

At the same time, this should work. Why do back-to-back messages on separate connections to the same URL fail? It took me quite a while to find this. There are no error messages. The first messages is simply ignored.

@SimonFrings
Copy link
Contributor

SimonFrings commented Jul 13, 2023

Hey @bobrundle, thanks for bringing this up 👍

I've fixed this by queuing up the messages to the same URL and sending them together on the same connection. A much better design, I admit.

Sounds good, this is definitely the better design to use here.

Why do back-to-back messages on separate connections to the same URL fail?

Hard to say to be honest. Is there no output (client or server side) at all for the "discarded" message? This might also depend on how much time each connection needs, maybe the second one is faster or is it possible to achieve different results (only one of them being "discarded" , depending on which connection comes first)? Any additional input is welcome in order to get some more information on this.

FYI: The Ratchet namespace for \ratchet\client\connect(…) should always be camelcase: \Ratchet\Client\connect(…)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants