D
D
Dmitry Tarasov2019-12-07 03:04:53
PHP
Dmitry Tarasov, 2019-12-07 03:04:53

How to implement real time chat and notifications?

Hello!
There was a problem on the site with the fact that every 10 seconds there was an ajax request that sent a php file, and he answered whether there was a notification in the form of a message from other users (notification of new messages), I started having a problem with the fact that online began to grow and mysql: too many connections error appeared.
I polled the file every 30 seconds, the error began to appear very rarely, but still appears, I thought it was nonsense, I have node.js, I thought, I already have the number of online in real time written on it.
I thought well, I need to write a notification about new messages and receiving new messages in private messages, update and delete messages in correspondence.
But I had a few questions.
1. How to send new messages and notifications exactly to the person to whom they belong?
2. How to understand that a person is authorized on the site?
3. How do I implement all this, I just can not imagine?
I have a stack on my site: php7, (node.js, express, socket.io), mysql, nginx, redis.
I was goofing around, came across different shared sessions php node.js, notification redis real time, redis session.
Krch, it really became even more unclear to me how to do this?
I want to have real-time messages between users, real-time notifications about new messages, how to understand whether a particular user is online and authorized now and whether there are new messages for him, if there is a notification, if the user is on the page in which he conducts his conversation with the person I want so that new messages arrive there in real time.
We need a simple and elegant solution without any frills.
Maybe I'm looking in the wrong place xs.
Please let me know, thanks in advance for all the help!!!

Answer the question

In order to leave comments, you need to log in

5 answer(s)
K
kafkiansky, 2019-12-07
@fast-je

centrifuge

R
Randewoo, 2019-12-07
@Randewoo

Googling what WebSockets are.
In php, I recommend using the Worker library, even the teapot will figure it out there.

E
Evgeny Devisok, 2019-12-07
@evgen_dev

There are several options:
1. Long-polling
2. Server Side Events
3. WebSocket I
recommend choosing the latter, but with the implementation of fallback on long polling (to support older browsers).
For authorization, you can, for example, use memcached as a session store in php and read it from nodejs by session id: https://stackoverflow.com/questions/24296212/php-n...

R
Roman Mirilaczvili, 2019-12-07
@2ord

online began to grow and an error appeared mysql: too many connections.

If we assume that connections with MySQL are not closed with node.js, then this is not nonsense. Then their number grows and reaches the limit. Either use a connection pool or reduce the connection time with the DBMS. And it's better to have both together.
And if these problems are not addressed, then you can increase the limit in the MySQL configuration and allocate more resources to the machine.
Regarding questions, you need to read what authentication and authorization are. And, having understood what it is, start implementing or looking for libraries.
Or do not fool yourself and just integrate a ready-made solution into the site.

X
xmoonlight, 2019-12-07
@xmoonlight

phreads

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question