R
R
Raccoon Raccoons2015-12-27 03:12:09
PHP
Raccoon Raccoons, 2015-12-27 03:12:09

How to make a chat on Memcache?

I am learning PHP a little.
Please tell me if I'm doing it right or not.
Task: Write a tete-a-tete Chat, an analogue of private messages in VK, but they can have many sessions open, there are no login bindings, we will assume that this is an anonymous chat
- message history is not needed, you need to store messages in RAM (new messages ) and periodically load it into the DOM for the user.
Closed the browser = messages were lost.
I know how to write such a thing in Mysql + php, but how can I speed this up by storing it in RAM?
My algorithm:
0) as I understand it, we can only work in 1 column, so we have to pervert with JSON formats
1) Let's say we have a 4 member, each of them has a unique hash as a key, and opposite it a data set and messages that are stored in the Memcache:
User_vasya_5ebf8364d17c8df7e4afd586c24f84a0 => 'null'
User_masha_8135a52f656cf46181ed78da134de84 => 'null'
User_zhenya_1f124cf46181ed78da13423111 => 'null '
User_rita_2131cf46181ed78da13423111 => 'null'
2) If Vasya wanted to chat with Masha, he sent her a message, thereby changing her value in the database, now the database looks like this:

User_vasya_5ebf8364d17c8df7e4afd586c24f84a0 => 'null'
User_masha_8135a52f656cf46181ed78da134de84 => 'multidimensional array of new JSON messages here'

something like this:
{
"User_vasya_5ebf8364d17c8df7e4afd586c24f84a0": "Hi",
"User_vasya_5ebf8364d17c8df7e4afd586c24f84a0": "How are you?",
"User_vasya_5ebf8364d17c8df7e4afd586c24f84a0", "Where are you from?"
}

2.1) Zhenya also decided to send a message to Masha, now her array looks like this:
{
"User_vasya_5ebf8364d17c8df7e4afd586c24f84a0": "Hello",
"User_vasya_5ebf8364d17c8df7e4afd586c24f84a0": "How are you?",
"User_vasya_5ebf8364d17c8df7e4afd586c24f84a0": "Where are you from?",
"User_zhenya_1f124cf46181ed78da13423111": "Hi, I jen"
}

3) Masha knocks every second on her line in Memcache via AJAX and checks if there is anything new there, if there is, then it downloads, unpacks, injects into the DOM and cleans up her Memcache
Questions:
1) It is very likely that Masha will send a request for deletion ("I added it to my DOM, you can clean up the array, it doesn't need to grow, it's stupid for the buffer"), at the very moment when Vasya sends her a new message. How to make a check?
Please tell me the best way to implement this.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Leonovich, 2015-12-27
@atlantech

If you do not need to save messages, then storage is not required. You will be able to manage with the means of your PL.
I recommend using the WebSocket protocol. This can be done in most PLs. If you want to do it in PHP, then look towards phpDaemon or reactphp. It will be enough for you to program a script that will accept connections and listen for sending messages from the client, then you can save the message to a common array (which, in fact, will be stored in memory) and distribute it to sockets to other users. All messages will be lost when the script terminates.
This is suitable for a training project, since the version I proposed does not take into account the release of resources, and with the growth of messages, the script will inevitably fall from memory. On a combat project, you will have to take care of this, then there will be a need for storage.
Read more articles on the internet about this, such as www.sitepoint.com/how-to-quickly-build-a-chat-app-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question