php comment using websocket

General comments are divided into these modes: 1. Building mode (such as NetEase News, which is more complicated) 2. Single new mode (such as the comment system of the laravel-china community, which is also a non-building mode)

  1. Single new mode is relatively simple (1. Single mode: whether adding a comment or replying to a comment is a new comment 2. Single reference mode: adding a new comment is a new comment, and the reply will be displayed below when replying to a comment which comment)

    Single Schema: (Database Design)

    id (self-incrementing i primary key, comment id)

    content (comment content)

    user_source (user source, WeChat, Weibo, app, etc.)

    user_code (user ID, transmitted from the app to identify the user)

    user_pic (user avatar)

    add_time (add time)

    level (whether top or not)

    Other fields...   

 

    Single reference pattern (database design):

    id (self-incrementing primary key, comment id) 

    content (comment content)

    add_time (comment time)

    r_id (the id of the respondent)

    r_content (respondent comment content)

    r_time (reply time)

    level (whether the reply is on top)

    Other fields........(Here I like to treat the reply comment and the reply comment as a comment, in the controller according to r_id (reply comment id) to get data from redis, if not, go to Check the database and store it in redis to set the expiration time, insert the obtained data into the database, or remove the field starting with r_ and replace it with parent_id, and obtain it through this traversal query)

 

    2. In this mode of building a building, I currently use the parent_id to recursively query. If the parent_id of the first commented is 0, the rest parent_id is the commented id, but this method consumes memory.

 

 

  The comment data is obtained from the front end. When we use websocket to send, it is best to compress the comment field, which can save server resources. For example, if there are 10,000 people online, one of them sends a comment, and the other 9,999 people have to see it, then After a long field is compressed, the total data size is reduced.

  When sending with websocket, it can be sent by post and redis subscription. I prefer to send by redis subscription. You can add comments to manage logs.

   The post method must have the server address of the websocket and the data to be sent. There must be a key value in the sent data that has been registered in the websocket.

    To publish the redis subscription method, you must first connect to the redis server, the key registered on the websocket, and the data to be sent.

  When sending a websocket comment, it is also divided into review before sending and sending after review. It is relatively simple to send after review. If there is data, enter the redis queue, and then brush into the database through the Linux crontab program. websocket

  If the data is sent first and then reviewed, it is also an advanced redis queue. If there are many concurrent comments, there must be two queues, the queue for sending websockets and the queue for not sending websockets, and then brush into the database through the Linux crontab program.

  crontab is executed at least once per minute. You can write a php file to execute it once per second. In the php file, write a for loop to execute, but you need to add such a piece of code to the front of the file.

      

  $cc = exec("ps -ef |grep '" . __FILE__ . "' | grep -v 'grep' | wc -l"); 
  if ($cc > 10) {
exit("program is running...") ;
  }
  This is to prevent a program from executing too much and causing the server to crash. I am a rookie. If there is anything wrong with the above, please point it out

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324966790&siteId=291194637