Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

private message using websockets

Tags:

php

websocket

How can we implement a private message system (client to client) using Websockets (and PHP)?

From what I understood, the server broadcasts the message and all of the clients can receive the message through connection.onmessage event handler. I was wondering if there is a way to send the message to only a targeted user in the websockets?

like image 795
Moe Avatar asked Jan 14 '23 22:01

Moe


1 Answers

When a client sends a messages (ws.send( message );) your WebSocket server will receive the message. The sockets that then you send that on to is determined entirely by your server code - your implementation.

To create a one-to-one chat you need a way of routing data between just the two clients involved in the chat. You also need a way of authenticating that only those two clients can receive that information.

WebSocket frameworks tend to provide an additional PubSub layer e.g. Pusher (who I previously worked for) do this using channels. You will find similar terminology along with 'subjects' and 'topics'.

Once you have a way of routing data (chat messages) between just two clients you then need to consider authenticating the subscription. More information can be found on that via this question which asks How do I create Private channel between two user. The question is about Ruby but is applicable to any technology.

like image 133
leggetter Avatar answered Jan 18 '23 11:01

leggetter