PHP实时聊天备份服务器生产问题

时间:2023-01-15 03:54:29

I have built out a real time chat application powered by PubNub. At present, I allow messages to be sent and received directly to and from a user through PubNub alone. However, I wish to "save" these messages by first pushing them to my server/db, built in PHP and MySQL, then upon successful entry, send to the user via PubNub.

我已经构建了一个由PubNub提供支持的实时聊天应用程序。目前,我允许仅通过PubNub直接向用户发送消息和从用户直接接收消息。但是,我希望通过先将它们推送到我的服务器/数据库(用PHP和MySQL构建)来“保存”这些消息,然后在成功输入后,通过PubNub发送给用户。

The setup of my present PHP server accepts the request and directs it to the proper case statment given the defined action passed into the request as:

我现在的PHP服务器的设置接受请求并将其指向正确的案例陈述,前提是已将定义的操作传递给请求,如下所示:

<?php

switch($action) 
{

    case "userLogin": 
        ...
    break;  


    case "signUpUser":
        ...
    break;  

        ...

    case "syncWithServer":
        ...
    break;  

?>

where each case statement allows for database insert and update and subsequent return upon the actions success given the data passed.

其中每个case语句允许数据库插入和更新,并在给定数据传递的操作成功后返回。

For a detailed example, we have the sendMessage case below. The data must be inserted and then return the latest sync_id, for that message just inserted, then sent back to the user so as to keep everything in sync:

有关详细示例,我们在下面有sendMessage案例。必须插入数据然后返回最新的sync_id,对于刚刚插入的消息,然后发送回用户以保持所有内容同步:

case "sendMessage":

    // Output is: {"message_sync_id":"4"}
    if ($userId = authenticateUser($db, $username, $password, $gcmregid)) 
    {   
        //echo "passed1";
        if (isset($_REQUEST['to_user_sync_id']))
        {
            //echo "passed2";a
             $touserid = $_REQUEST['to_user_sync_id'];               
             $unescapedMessage = $_REQUEST['message_text'];
             $message = mysql_real_escape_string($unescapedMessage);             
             $campaign = $_REQUEST['shared_campaign_id'];
             $location = $_REQUEST['shared_campaign_location_id'];

             if($stmt1 = $db->prepare("INSERT INTO `messages` (`message_sync_id`,`from_user_sync_id`, `to_user_sync_id`, sent_dt, `message_text`,`shared_campaign_id`,`shared_campaign_location_id`) 
                                      SELECT 
                                      CASE 
                                      WHEN max(message_sync_id) is null
                                      THEN 0
                                      ELSE max(message_sync_id)
                                      END + 1
                                      , ?
                                      , ?
                                      , NOW()
                                      , ?
                                      , ?
                                      , ?
                                      from messages;"))
             {
                $stmt1->bind_param("sssss", $userId,$touserid,$message,$campaign,$location);
                $stmt1->execute();
                $stmt1->close();

                $sqlMaxSyncId = "select max(message_sync_id) as message_sync_id from messages;";

                if($resultMaxID = $db->query($sqlMaxSyncId))
                {

                    $row = $resultMaxID->fetch_assoc();

                    $out = json_encode(array('message_sync_id' => $row['message_sync_id']));

                    error_log("sendMessage", 3 , $row['message_sync_id']);

                }
                else
                {
                    $out = FAILED;
                }           

             }
             else
             {
                $out = FAILED;
             }

        }
        else
        {
            $out = FAILED;
        }   
    }
    else
    {
        $out = FAILED;
    }   
break;

Given chat applications require very low latency, what production problems may this formulation run into that I may not see during very low usage testing?

鉴于聊天应用程序需要非常低的延迟,这个配方会遇到哪些生产问题,我可能在极低的使用率测试中看不到?

How can these be alleviated?

怎么能减轻这些?

Would Heroku be an appropriate environment to deploy such a server?

Heroku是否适合部署这样的服务器?

1 个解决方案

#1


0  

The save to database (which I guess is "syncWithSErver") could become a bottleneck if many users are chatting at the same time as I guess, that case statement executes a database query, a database that would be overloaded, and thus, slowing down the message dispatch.

如果许多用户在我猜的同时聊天,那么保存到数据库(我猜是“syncWithSErver”)可能会成为瓶颈,该case语句执行数据库查询,数据库会过载,从而减慢速度消息发送。

Instead of saving them directly, use any memory cache, such as memcache or redis to store them temporary, and use a background job (a php script attached to cron, for example) that will keep saving them.

而不是直接保存它们,使用任何内存缓存(例如memcache或redis临时存储它们),并使用后台作业(例如附加到cron的php脚本)来保存它们。

#1


0  

The save to database (which I guess is "syncWithSErver") could become a bottleneck if many users are chatting at the same time as I guess, that case statement executes a database query, a database that would be overloaded, and thus, slowing down the message dispatch.

如果许多用户在我猜的同时聊天,那么保存到数据库(我猜是“syncWithSErver”)可能会成为瓶颈,该case语句执行数据库查询,数据库会过载,从而减慢速度消息发送。

Instead of saving them directly, use any memory cache, such as memcache or redis to store them temporary, and use a background job (a php script attached to cron, for example) that will keep saving them.

而不是直接保存它们,使用任何内存缓存(例如memcache或redis临时存储它们),并使用后台作业(例如附加到cron的php脚本)来保存它们。