Note that there are some explanatory texts on larger screens.

plurals
  1. POSending websocket data to arbitrary client (based on e.g. username)
    primarykey
    data
    text
    <p>Is the following possible with websockets:</p> <ul> <li>UserA and UserB are users of a site.</li> <li>UserA logs in to the site and starts up a websocket connection.</li> <li>UserB has a message for UserA, so they submit a form that is handled by some script on the server (doesn't matter if it's through WS, XHR or just a plain form)</li> <li>The server sends the message to UserA through their websocket connection - without the server code just checking the database continually for new messages.</li> </ul> <p>Ideally the script that handles the sending of the message would do everything - put the message into the database and send it to the recipient's websocket connection as well.</p> <p>Edit: here is the actual scenario I have in mind for this kind of functionality:</p> <p>I am developing a chess site which currently uses long-polling for updating the user when their opponent moves, and a few other things like a chat box beneath the board.</p> <p>Here's the PHP script that the user calls to make a move, with code added (near the end) to illustrate what I want to do:</p> <p>/xhr/move.php</p> <pre><code>&lt;?php require_once "base.php"; require_once "Data.php"; require_once "php/livechess/LiveGame.php"; require_once "php/init.php"; $result=false; if($session-&gt;user-&gt;signedin()) { $q=Data::unserialise($_GET["q"]); if(isset($q["gid"]) &amp;&amp; isset($q["fs"]) &amp;&amp; isset($q["ts"])) { $colour=Db::cell(" select colour from seats where user='{$session-&gt;user-&gt;username}' and gid='{$q["gid"]}' and type='".SEAT_TYPE_PLAYER."' "); if($colour!==false) { $promote_to=QUEEN; if(isset($q["promote_to"])) { $promote_to=$q["promote_to"]; } $game=new LiveGame($q["gid"]); if($game-&gt;position-&gt;active===$colour) { if($game-&gt;move($q["fs"], $q["ts"], $promote_to)-&gt;success) { $result=$game-&gt;history-&gt;main_line-&gt;last_move-&gt;mtime; $game-&gt;check_premoves(); $game-&gt;save(); /* here is where the functionality I'm talking to would come in: (some pseudocode for brevity) */ $opponent_username=Db::cell("select user from seats where colour=".opp_colour($colour)." and etc"); send_websocket_data($opponent_username, Data::serialise($game-&gt;history-&gt;main_line-&gt;last_move)); } } } } } echo Data::serialise($result); ?&gt; </code></pre> <p>I have no idea what the implementation of the send_websocket_data function would be like, except that it would take a username and some data to send.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload