Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending message to client with Web Sockets

Tags:

php

websocket

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?

like image 630
Christoffer Avatar asked Oct 01 '18 20:10

Christoffer


People also ask

Can a WebSocket server send message to client?

The Message event takes place usually when the server sends some data. Messages sent by the server to the client can include plain text messages, binary data, or images. Whenever data is sent, the onmessage function is fired. This event acts as a client's ear to the server.

How do I send a WebSocket message to a server?

To send a message through the WebSocket connection you call the send() method on your WebSocket instance; passing in the data you want to transfer. socket. send(data); You can send both text and binary data through a WebSocket.

Should I use WebSockets for chat?

WebSockets are a great fit for applications like chats or simple games. Chat sessions are usually long-lived, with the client receiving messages from other participants over a long period of time. Chat sessions are also bidirectional – clients want to send chat messages, and see chat messages from others.

What is WebSocket message?

The WebSocket API is an advanced technology that makes it possible to open a two-way interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.


1 Answers

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.

like image 61
Sven Liivak Avatar answered Sep 30 '22 05:09

Sven Liivak