If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Update of a page based on MySQL events

Started by solarjimmy, Apr 24, 2023, 06:14 AM

Previous topic - Next topic

solarjimmyTopic starter

The situation entails working with an application that only supports MySQL, a MySQL Server 5.1 and a web server comprised of apache, nginx, and php along with memcached.

 The application writes data to MySQL at a rate of up to three records per second. To store updated data in memcache, MySQL uses a trigger which keeps the data stored for 24 hours. The aim is to have the updated record displayed on the page once it has been updated.

To accomplish this, a self-written PHP script, a comet or nginx module must be used to initiate data transfer from the web server to the client, or find a way to write data from MySQL or memcache to the channel to notify the web server that a change has occurred.
  •  


Mycrib

This solution may be helpful if you follow these steps:

1. Write a UDF function in C for MySQL that can be called from a trigger, using the dоcumentation provided by dev.mysql.com. Call this function push_message(...).

2. Implement a message queue using shared memory and ensure it is lock-free to prevent any impact on MySQL performance. Your push_message function should write messages to this queue.

3. Write an external program, preferably in Python, to read messages from the shared memory queue and write them to the push server. You can use dklab as your push server, which has a Python API implemented at github.com/DmitryKoterov/dklab_realplexor/tree/master/api/python.

4. The push server will then send JS messages to clients in their browser.
  •  

himachaldesk

Is it feasible to modify the function that inserts an entry into the database by including a call to retrieve all information from the same database and utilize Ajax to publish it on the webpage? This will result in the following sequence:

- Verify the capability of writing to the database
- If successful, add an entry to the database and instantly initiate a request for the required data
- After receiving the data, store it in a variable and use Ajax to load it to the desired webpage.

By doing so, you can monitor the condition of the database in real-time. To handle errors such as server breakdown or database writing incapability, you can incorporate a handler to perform specific actions.
  •  

katiezeal

Consider using Google to find "lib_mysqludf_sys", which may be useful in your situation.
As previously stated, you have the option of creating your own User-Defined Function (UDF), or utilizing a pre-existing one found at www.mysqludf.org/lib_mysqludf_sys/index.php, which can enable calling an external program or script through a trigger mechanism.
  •  

anilkh7058

  •  

berilo

One possible approach to achieve this would be to use a combination of MySQL triggers, memcached, and a PHP script. Here's a high-level overview of how this could work:

1. Set up a MySQL trigger: Whenever a record is updated in MySQL, the trigger will be triggered. Inside the trigger, you can write the updated data to memcached.

2. Implement a PHP script: This PHP script should continuously monitor memcached for changes. It can use a loop with a sleep timer to periodically check for new data. When new data is found in memcached, the PHP script can then send a notification to the web server or directly to the client using a technique like Comet or long polling.

3. Configure web server: You'll need to configure your web server (Apache or Nginx) to handle the notifications sent by the PHP script. This might involve setting up specific endpoints or configuring URL rewriting rules.

4. Update the web page: Finally, on the client-side, you'll need to implement JavaScript code that listens for these notifications and updates the page accordingly. This could involve handling specific events that are triggered by the server.



To expand on the previous response, here are some more details and considerations for each step:

1. MySQL Trigger:
- Create a trigger that is fired after an update on the specific table(s) you want to track.
- In the trigger, use MySQL's built-in memcached support to store the updated data in memcached. You can use the `memcached_set()` function inside the trigger to achieve this.

2. PHP Script:
- Write a PHP script that runs continuously or repeatedly checks memcached for new data. You can use a loop with a sleep timer to periodically check for updates.
- Inside the PHP script, use the `memcached_get()` function to retrieve the data that was stored in memcached by the trigger.
- If new data is found, you can send a notification to the web server or directly to the client using techniques like Comet or long polling. This could involve making an HTTP request to the appropriate endpoint on the web server.

3. Web Server Configuration:
- Configure your web server (Apache or Nginx) to handle the notifications sent by the PHP script.
- This might involve setting up a specific URL or endpoint that the PHP script can connect to and send the notification.
- Depending on your preference, you can choose between Comet (using techniques like Server-Sent Events or WebSocket) or long polling (making periodic AJAX requests).

4. Update the Web Page:
- On the client-side, implement JavaScript code that listens for the notifications sent by the server.
- When a notification is received, update the relevant parts of the web page to reflect the changes.
- This could involve manipulating the DOM dynamically or using frameworks/libraries like React or Vue.js to handle the updates efficiently.

Remember to handle concurrency and handle edge cases like multiple clients connecting simultaneously or handling errors gracefully in case of network issues or failures to read from memcached.

Overall, this approach allows you to detect changes in MySQL data and push notifications to clients, ensuring that the updated records are displayed on the web page as soon as they occur.
  •  

ananya34

You're basically polling memcache or relying on PHP scripts to do long-polling or comet hacks, which is a nightmare for scalability and latency.

Instead, ditch triggers and implement a lightweight websocket or SSE layer in PHP or nginx to push real-time updates. Polling memcache every few seconds is just a waste of resources and adds lag. Upgrade your stack or at least use a message queue like Redis or RabbitMQ for event-driven pushes rather than this trigger-to-memcache workaround.
  •  



If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...