C
C
chrome1232014-08-26 17:11:02
MySQL
chrome123, 2014-08-26 17:11:02

How to extract new entry and NodeJS + mysql bases?

Tell me how to correctly implement the output of a new row from the database on the initiative of the server?
The server is connected to the client via sockets.
Tried to do so.
latest - The ID of the last message printed.
message[0] - My ID
message[1] - Interlocutor ID

setInterval(function(){
connection.query('SELECT * FROM messages  WHERE id>'+latest+' &&  ((sender='+message[0]+' && recipient='+message[1]+') || (sender='+message[1]+' && recipient='+message[0]+')) ORDER BY id DESC', function(err, rows, fields) {
            if (err) throw err;
// Отправка данных пользователю
        });

        },1);

But this option is not suitable. Because it turns out that way. If there are a lot of users. Then the Record does not have time to go to the client, and setInterval goes already in 2 circles. And the record is duplicated 2-3 times. Although I have been fighting with this problem for quite a long time and on other forums I was assured that this is a good option for implementing my task.
I also tried to implement this task through Trigers and Stored Procedures. But these attempts were also unsuccessful.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Daniel, 2014-08-28
@ZOTTCE

You must explicitly have events responsible for sending a message by the client.
Why don't you send a notification there if the user to whom the message is addressed is connected.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question