Php+mysql do backstage, how to realize user login, push message

Source: Internet
Author: User

This is probably the case:
After a landing, you can send a message to B, for example, select B's work number

After sending, b login, can be in the department message there will be a little red dot, prompting a message

My train of thought is this: when a press send, it will write a record in the database that holds the message, and the status code of the message is set to unread, the recipient is B, when B is logged in, because the status code is unread, it will display the prompt message. But how does this hint come true?

This is the database of my messages:

If I send the same message to more than one person at a time, should I write more than one record? Write a message to the database to loop?
Another question is: If my message contains a picture, is the picture to be placed in a folder?
Hope to give some advice, thank you!

Reply content:

This is probably the case:
After a landing, you can send a message to B, for example, select B's work number

After sending, b login, can be in the department message there will be a little red dot, prompting a message

My train of thought is this: when a press send, it will write a record in the database that holds the message, and the status code of the message is set to unread, the recipient is B, when B is logged in, because the status code is unread, it will display the prompt message. But how does this hint come true?

This is the database of my messages:

If I send the same message to more than one person at a time, should I write more than one record? Write a message to the database to loop?
Another question is: If my message contains a picture, is the picture to be placed in a folder?
Hope to give some advice, thank you!

You're sure you just see the message when the user logs in, right? If it is, then look down.

1. How to Prompt for messages

In your table, message_id should be the primary key for this watch, and each message corresponds to a message_id. When a user logs in, query the message table receive_id and Message_status, that is, query the number of users logged in the unread message, if the number >0, indicating that there are unread messages, login successful jump, there is a mes_ The Status field is also passed in the same time. For example, jump to the Home page (index), index and then determine the value of Mes_status, to the user to prompt.

2. If you send it to more than one person

In fact, your message table of the primary key is message_id, and sent to several people is not related, when he logged in, only query the user himself as the receiver, the number of messages received. For example, there are some data in the data table:

message_id send_id Message_date Message_title Message_content receive_id Message_status
1 111 2015-06-25 13:00:00 Hello Hello World 222 1
2 111 2015-06-25 14:00:00 May China, hello 333 1
3 444 2015-06-26 11:00:00 Wenzi Wenzi 222 1
4 666 2015-06-26 16:00:00 Hello Hello World 222 1

Users 111 can send messages to multiple users 222 , 333 do not conflict, and users can receive messages from many other users, for example 222 . When the user (userid== ' 222 ') logs on, the message is queried (Receive_id==userid).

3. What to do if there is a picture in the message

Your inference is right, you should first put the user upload pictures into a file, and then return to the User a picture link, the user inserted the image link into the message.

This is going to use push.
Tell the truth. PHP is not suitable for push. To use Ajax polling. This is inefficient and prone to problems.
There is a very good solution here.
http://segmentfault.com/q/1010000000589829/a-1020000000591493

Send a message can be directly write DB attachment of course, there is a static file server to upload the generated URL is also convenient for later expansion;
As for the push to the browser, the client must use a long connection (recommended), WebSocket (recommended) or Ajax polling method to achieve. Monitor state Change Interface

The field of the message receiver can be stored 1,2,3,4 this way, sending multiple people does not require writing multiple data.

To query the message push table for unread messages when logging in

If you need to prompt online, you need to do front-end Ajax polling

If the real-time requirement is not high, the front end can poll the message table every 5 minutes ajax.

If you want to insert more than one record, it is recommended to turn on the transaction, loop the INSERT, commit the transaction, and speed up the insertion.

If you do not want to insert multiple records, the message receiver's field receive_id save the recipient's ID sequence (1,2,3,4), the Message Status field Message_status Save the recipient ID sequence (2,3) of the unread message, as the @ lunchbox says, Message_ The status initial value is (1,2,3,4) and the user ID of the viewed message is removed from it. Queries can be made using the MySQL intrinsic function find_in_set (return to the location found, starting at 1), such as:

$user_id = 2;$sql = <<
  
   < code="">

数据量比较大的话,FIND_IN_SET可能会存在性能问题,建议定时清理message表过期的消息。

workerman

可以用 workerman 做

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.