John Davidson

javascript - SSE Stream save to mysql

0 comments
Message:


I have a SSE stream and I need to save the results to a Mysql table. I can stream the data from the SSE endpoint in the browser and save to Mysql. The issue is I need a php script to run every 10 minutes in the command line and store the result. The script I am using is below, it only works in the browser - I assume because its javascript.


var source = new EventSource("https://sseAPIUrl.com");
source.onmessage = function(event) {
document.getElementById("result").innerHTML += event.data + "<br>";
var posData = event.data;

$(document).ready(function(){
$.ajax({
data:{
streamData:posData
},
type: "post",
url: "saveSSEFeed.php",
success: function(data){
alert(data);
}
});
});
};


My Answer:
'http://example.com/stream');

source.onmessage = function(event) {
var data = JSON.parse(event.data);

// Save data to MySQL
var xhr = new XMLHttpRequest();
xhr.open('POST', 'save_data.php', true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify(data));
};

Rate this post

4 of 5 based on 5673 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us