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

Keep socket connection open , and send push messages from other classes #33

Closed
harveyslash opened this issue Jun 17, 2016 · 3 comments
Closed
Labels

Comments

@harveyslash
Copy link

harveyslash commented Jun 17, 2016

I have made a helper class like :

class SocketPush
{
    /**
     * Helper Function to broad a message.
     * The metadata required to send message should be part of message itself
     * Example usage:
     * SocketPush::SendBroadcast(SocketPush::constructMessage(PusherServer::$PUSHABLE_USER_PREFIX . '1'
     * , PusherServer::$ACTION_PUSH, "coming your way"));
     * @param $msg string
     */
    public static function SendBroadcast($msg)
    {
        $loop = Factory::create();
        $connector = new Connector($loop);
        $connector('ws://127.0.0.1:8080', [], ['Origin' => 'origin'])
            ->then(function (WebSocket $conn) use ($msg, $loop) {
                $conn->remoteAddress = '127.0.0.1';
                $conn->on('close', function ($code = null, $reason = null) {
                });
                $conn->send($msg);
                $conn->close();
            }, function (\Exception $e) use ($loop) {
                echo $e->getMessage();
                $loop->stop();
            });
        $loop->run();
    }
}

I am using push notification using ratchet, and so , whenever I need the server to send a message to a client, I call the sendBroadcast method of this client.
What this does is, everytime a message needs to be sent, it initiates a connection , then sends the message, then closes it(this sounds like unnecessary overhead)

Is is possible to keep the connection open at all times (if for some reach it disconnects, reconnect), and , call the same send broadcast method from other classes to send message ?

@erleeman
Copy link

erleeman commented Oct 22, 2016

You have to run all your code inside the loop. Or use a message broker like ZMQ.

http://socketo.me/docs/push

And (not especially your use case but same problem):

reactphp/http#75

@cboden
Copy link
Member

cboden commented Jan 1, 2017

Your SocketPush class is run inside your website (not within your Ratchet) app, correct? If so what you're doing is correct. It's the same as the push tutorial just using a different transport. With each client request to your website you do have to open and new connection every time as the environment is destroyed at the end of the request.

@cboden cboden added the question label Jan 2, 2017
@mbonneau
Copy link
Member

mbonneau commented Oct 3, 2017

Closing this because there has been no activity in a while.

@mbonneau mbonneau closed this as completed Oct 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants