i'm new to websockets. Ive just completed my first multipurpose socket server, which serves 3 types of messages in the same socket. This server's intended usage is as follows:
chat messages
real time market data
user specific alerts
The first two functions work great and i'm totally happy, however i am at a total loss when it comes to identifying and linking peers in the connection to specific accounts, so that i can serve pending alert notices to specific users.
basically, i store the alert messages in a database table like so:
Table: uc_notifications
account(varchar50) | message(varchar255) | id(primary,AI) | seen(default 0)
i want to store the peers in a table like so:
uc_notification_peers
account(varchar50) | peer_id(unique,varchar100) | id(primary,AI)
Basically, the problem i'm having is storing the peer's info in the db, and retrieving it so i can send the message to the select peer while reading the notification table. Everything else in the script worked.
The first thing i tried, was passing the account name to the websocket using onopen. this crashes the websocket.
The next thing i tried was accessing the user session through globals pertaining to the ip address. didn't work either, and resulted in messages being received by all accounts on same ip( i was testing on localhost in all 3 browsers).
The last thing i tried, was the first method. i set a timeout of 2 seconds on the on open event, then sent the account message. i was able to finally store a peer in the database! but, to my chagrin when i checked the database, it said ("resource id#6). i tried casting the $clients array to globals but its empty everytime. i googled around a bit, then i tried serializing the array. it all works until i get to the part where i send the message to the unique peer.
here's the function for sending the message
function send_message_single_client($msg,$client)
{
@socket_write($client,$msg,strlen($msg));
return true;
}
here's an example of where i use this function to push the last 100 messages to the chatbox of the new peer.
if (in_array($socket, $changed)) {
$socket_new = socket_accept($socket);
$clients[] = $socket_new;
$header = socket_read($socket_new, 1024);
perform_handshaking($header, $socket_new, $host, $port);
socket_getpeername($socket_new, $ip);
$found_socket = array_search($socket, $changed);
//need to push last 100 messages to new peer.
$query = $mysqli->query("SELECT * FROM (SELECT * FROM uc_chat_msg WHERE `hidden`='0' ORDER BY `id` DESC LIMIT 100) as last100 ORDER BY id");
while($row = $query->fetch_assoc()) {
$response_text = mask(json_encode(array('type'=>'usermsg', 'name'=>security($row["username"]), 'message'=>security($row["message"]), 'color'=>security($row["color"]))));
send_message_single_client($response_text, $socket_new);//send old messages to the new client
}
$query->close();
unset($changed[$found_socket]);
}
That was a few weeks ago, and i put the project on the backburner so i could move on and focus on other things. Since, i've moved to a development vps to accomadate the growing size/complexity of the application. instead of php, i'm using facebooks HHVM, not relevant but thought i would mention it, since it has a few quirks that forced me to alter my code a bit.