2

Is it possible to send message (for example using alert) to all users when admin changed something in database?

situation: Users browsing car offers and while doing this admin changed price of few offers --> users gets notifications.

1
  • I don't know if you can directly watch for DB changes, but you can emit a Socket.io event after each time you update the DB and listen to that on the client side. Commented Apr 13, 2018 at 13:41

2 Answers 2

1

Just couple the event of the database update to an emit like this:

Backend

io.on('connection', (socket) => {
    console.log('A user connected');

    // handling event from the front-end:
    socket.on('clientEvent', function(data) {
        // Database update happens before this
        socket.emit('databaseUpdate', { description: 'Database is updated'});
     });
});

This way every time a database update happens a new event will be emitted to the frontend to all the users which are connected. Your frontend now can listen to it as follows (the frontend who is connected listened to emitten databaseUpdates from the backend):

Frontend

var socket = io();
// now we just log the updated data but in this callback you provide your own implementation.
socket.on('databaseUpdate', (data) => console.log(data.description));

Hopefully you find this answer usefull more info here

source1

Source2

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for reply, but the real problem for me here is how to watch for db changes and how to write it in 'Backend'. I know how to show messages between server-client. The best option would be if admin update db in phpmyadmin and after that there is alert. So I guess i need here some kind of watcher or maybe there is some script? Optionally would be also ok displaying message after constant query...
0

You can use socket.blast() at the end of each db operation.

So, if any user is listening to the blasted message, you can make the API call so that it fetches the new record.

[http://node-machine.org/machinepack-sockets/blast][1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.