Christoffer
Christoffer

Reputation: 492

Sending message to client with Web Sockets

I'm using https://github.com/orchidsoftware/web-socket in my laravel project and I want to send messages to clients connected.

So far I've followed the README and got the server up and running - I get the alert "The connection is established.".

But when I try to send a message to the client, nothing happens. I've created a sendMessageToAll function and tried to call it both from onOpen and from another controller:

public function onOpen(ConnectionInterface $conn)
{
    $this->clients->attach($conn);
    $this->sendMessageToAll("message");
}

public function sendMessageToAll($msg){
    foreach ($this->clients as $client) {
        $client->send($msg);
    }
}

And from another controller:

public function test() {
    $ws = new WebSocketClass();
    $ws->sendMessageToAll("testing");
}

Is there something I'm missing in order to get it up and running?

Upvotes: 6

Views: 5933

Answers (4)

MIT
MIT

Reputation: 140

i am using Swoole extension for socket operations, and i've never get that issua like that. Just try it on your services; https://www.swoole.co.uk/

Upvotes: 0

Marshal
Marshal

Reputation: 11081

If you have not read it already, there is a closed issue on this topic that states the following.

you must send a message using php to the working socket. I use Pawl for such purposes.

Question: How to send message from server-side? #11

https://github.com/orchidsoftware/web-socket/issues/11

Upvotes: 0

McBern
McBern

Reputation: 549

Your client should be connection to your socket server via Javascript as the documentation says:

var socket = new WebSocket("ws://localhost");

socket.onopen = function() {
  alert("The connection is established.");
};

socket.onclose = function(event) {
  if (event.wasClean) {
    alert('Connection closed cleanly');
  } else {
    alert('Broken connections'); 
  }
  alert('Key: ' + event.code + ' cause: ' + event.reason);
};

socket.onmessage = function(event) {
  alert("The data " + event.data);
};

socket.onerror = function(error) {
  alert("Error " + error.message);
};


//To send data using the method socket.send(data).

//For example, the line:
socket.send("Hello");

And you need to run your socket server first like this:

    Create socket listener:

To create a new listener, you need to

php artisan make:socket MyClass

In the folder app/HTTP/Socket/Listener create template Web listener socket

After creating a need to establish a route which Is located routes/socket.php

//routing is based on an Symfony Routing Component
$socket->route('/myclass', new MyClass, ['*']);

To launch the web-socket, use the command:

php artisan socket:serve

Then you follow the Authorization in the back end as stated in the in github.

Upvotes: 0

Sven Liivak
Sven Liivak

Reputation: 1403

From another controller? You cannot access process (php script) running in another thread (that's you server). Socket server is a hub between connected clients, waits for a message and if receives one, sends it out again (if told to do so). In other words - if you want to send message to all connected clients you'll have to be one of them.


Your initial code looks ok, server should be up and running. So, test it.

Easiest way is to open some telnet connections to your socketserver and start messaging.

public function onOpen(ConnectionInterface $conn)
{
    $this->clients->attach($conn);
    $msg = "Connection established!\n"
    echo $msg;                        // send server log to shell
    sendMessageToAll($msg);           // your initial function should be working
}

public function onMessage(ConnectionInterface $from, $msg) {
    echo "Message from: {$conn->resourceId} received!\n";  //log again
    sendMessageToAll($msg);
}

public function onError(ConnectionInterface $conn, \Exception $e) {
    echo "Error: {$e->getMessage()}\n";
    $conn->close();
}

Also, debug your sending function:

public function sendMessageToAll($msg){
    foreach ($this->clients as $client) {
        echo "Sending message to {$client->resourceId} \n";
        $client->send($msg);
    }
}

Now, open some telnet connections to your socketserver port (getting noticed in server console):

telnet 127.0.0.1 8080

and send message from one of them. Again, you should get notice from server and receive the message by each telnet client.

Upvotes: 4

Related Questions