How to make a webpage with server sent event source using php sockets not freeze up












0















My home.php page has various hrefs. When one of these hrefs is clicked, it opens up a second window called sseReceiver.php. The home.php window stays open too. sseReceiver.php uses a server sent event source called theSource.php to receive events and display them in a table.



theSource.php establishes a socket connection to a socket that was created using a Java server.
When the Java server generates a message, theSource.php receives it and flushes it. sseReceiver.php picks up this message and displays it.



This works well.



However, while sseReceiver.php is open, if I click on any other href on my home.php page, the page is stuck loading and ultimately gives a 504 error.



Question
Why am I unable to access the other links on home.php while sseReceiver.php is running?
Is there a way to run theSource.php in a different thread so that I can still access the rest of the website simultaneously?



Appreciate your suggestions and help.



Thank you.



Code for theSource.php



session_start();    
header("Content-Type: text/event-streamnn");
header('Cache-Control: no-cache');
set_time_limit(0);
ob_end_flush();
ob_implicit_flush(1);

function sendMessage($message)
{
$explodedData = explode(',', $message);
$data = array();
foreach ($explodedData as $result)
{
$b = explode('^', $result);
$data[trim($b[0])] = trim($b[1]);
}
//sse stuff
echo("retry: 3000n");
echo('data: '. json_encode($data));
echo "nn";
flush();
}


$serverAddress=SERVER_ADDRESS;
$serverListeningPort=SERVER_PORT;
//make a connection and get a socket object
if ( ($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === FALSE )
{
echo "socket_create() failed: reason: " .socket_strerror(socket_last_error());
}
else
{
echo("socket create was successful");
echo("<br>");
}
echo ("Attempting to connect to host");
echo("<br>");
if ( ($result = socket_connect($socket, $serverAddress, $serverListeningPort)) === FALSE )
{

echo ("socket_connect() failed. Reason:".socket_strerror(socket_last_error($socket)));
}
echo ("Reading response:");
$message="";

while(true)
{
$message=socket_read($socket, 300);
if($message!=='')
{
sendMessage($message);
}
}


Code for sseReceiver.php



var evtSource=new EventSource("theSource.php");     

evtSource.onmessage=function(event)
{
var payload=JSON.parse(event.data);
// code to display payload on webpage in a table
}









share|improve this question























  • The infinite while loop seems to be causing this issue. Is there a way to make that loop run on a different thread or in the background?

    – dollarSquare
    Nov 24 '18 at 15:17
















0















My home.php page has various hrefs. When one of these hrefs is clicked, it opens up a second window called sseReceiver.php. The home.php window stays open too. sseReceiver.php uses a server sent event source called theSource.php to receive events and display them in a table.



theSource.php establishes a socket connection to a socket that was created using a Java server.
When the Java server generates a message, theSource.php receives it and flushes it. sseReceiver.php picks up this message and displays it.



This works well.



However, while sseReceiver.php is open, if I click on any other href on my home.php page, the page is stuck loading and ultimately gives a 504 error.



Question
Why am I unable to access the other links on home.php while sseReceiver.php is running?
Is there a way to run theSource.php in a different thread so that I can still access the rest of the website simultaneously?



Appreciate your suggestions and help.



Thank you.



Code for theSource.php



session_start();    
header("Content-Type: text/event-streamnn");
header('Cache-Control: no-cache');
set_time_limit(0);
ob_end_flush();
ob_implicit_flush(1);

function sendMessage($message)
{
$explodedData = explode(',', $message);
$data = array();
foreach ($explodedData as $result)
{
$b = explode('^', $result);
$data[trim($b[0])] = trim($b[1]);
}
//sse stuff
echo("retry: 3000n");
echo('data: '. json_encode($data));
echo "nn";
flush();
}


$serverAddress=SERVER_ADDRESS;
$serverListeningPort=SERVER_PORT;
//make a connection and get a socket object
if ( ($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === FALSE )
{
echo "socket_create() failed: reason: " .socket_strerror(socket_last_error());
}
else
{
echo("socket create was successful");
echo("<br>");
}
echo ("Attempting to connect to host");
echo("<br>");
if ( ($result = socket_connect($socket, $serverAddress, $serverListeningPort)) === FALSE )
{

echo ("socket_connect() failed. Reason:".socket_strerror(socket_last_error($socket)));
}
echo ("Reading response:");
$message="";

while(true)
{
$message=socket_read($socket, 300);
if($message!=='')
{
sendMessage($message);
}
}


Code for sseReceiver.php



var evtSource=new EventSource("theSource.php");     

evtSource.onmessage=function(event)
{
var payload=JSON.parse(event.data);
// code to display payload on webpage in a table
}









share|improve this question























  • The infinite while loop seems to be causing this issue. Is there a way to make that loop run on a different thread or in the background?

    – dollarSquare
    Nov 24 '18 at 15:17














0












0








0








My home.php page has various hrefs. When one of these hrefs is clicked, it opens up a second window called sseReceiver.php. The home.php window stays open too. sseReceiver.php uses a server sent event source called theSource.php to receive events and display them in a table.



theSource.php establishes a socket connection to a socket that was created using a Java server.
When the Java server generates a message, theSource.php receives it and flushes it. sseReceiver.php picks up this message and displays it.



This works well.



However, while sseReceiver.php is open, if I click on any other href on my home.php page, the page is stuck loading and ultimately gives a 504 error.



Question
Why am I unable to access the other links on home.php while sseReceiver.php is running?
Is there a way to run theSource.php in a different thread so that I can still access the rest of the website simultaneously?



Appreciate your suggestions and help.



Thank you.



Code for theSource.php



session_start();    
header("Content-Type: text/event-streamnn");
header('Cache-Control: no-cache');
set_time_limit(0);
ob_end_flush();
ob_implicit_flush(1);

function sendMessage($message)
{
$explodedData = explode(',', $message);
$data = array();
foreach ($explodedData as $result)
{
$b = explode('^', $result);
$data[trim($b[0])] = trim($b[1]);
}
//sse stuff
echo("retry: 3000n");
echo('data: '. json_encode($data));
echo "nn";
flush();
}


$serverAddress=SERVER_ADDRESS;
$serverListeningPort=SERVER_PORT;
//make a connection and get a socket object
if ( ($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === FALSE )
{
echo "socket_create() failed: reason: " .socket_strerror(socket_last_error());
}
else
{
echo("socket create was successful");
echo("<br>");
}
echo ("Attempting to connect to host");
echo("<br>");
if ( ($result = socket_connect($socket, $serverAddress, $serverListeningPort)) === FALSE )
{

echo ("socket_connect() failed. Reason:".socket_strerror(socket_last_error($socket)));
}
echo ("Reading response:");
$message="";

while(true)
{
$message=socket_read($socket, 300);
if($message!=='')
{
sendMessage($message);
}
}


Code for sseReceiver.php



var evtSource=new EventSource("theSource.php");     

evtSource.onmessage=function(event)
{
var payload=JSON.parse(event.data);
// code to display payload on webpage in a table
}









share|improve this question














My home.php page has various hrefs. When one of these hrefs is clicked, it opens up a second window called sseReceiver.php. The home.php window stays open too. sseReceiver.php uses a server sent event source called theSource.php to receive events and display them in a table.



theSource.php establishes a socket connection to a socket that was created using a Java server.
When the Java server generates a message, theSource.php receives it and flushes it. sseReceiver.php picks up this message and displays it.



This works well.



However, while sseReceiver.php is open, if I click on any other href on my home.php page, the page is stuck loading and ultimately gives a 504 error.



Question
Why am I unable to access the other links on home.php while sseReceiver.php is running?
Is there a way to run theSource.php in a different thread so that I can still access the rest of the website simultaneously?



Appreciate your suggestions and help.



Thank you.



Code for theSource.php



session_start();    
header("Content-Type: text/event-streamnn");
header('Cache-Control: no-cache');
set_time_limit(0);
ob_end_flush();
ob_implicit_flush(1);

function sendMessage($message)
{
$explodedData = explode(',', $message);
$data = array();
foreach ($explodedData as $result)
{
$b = explode('^', $result);
$data[trim($b[0])] = trim($b[1]);
}
//sse stuff
echo("retry: 3000n");
echo('data: '. json_encode($data));
echo "nn";
flush();
}


$serverAddress=SERVER_ADDRESS;
$serverListeningPort=SERVER_PORT;
//make a connection and get a socket object
if ( ($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === FALSE )
{
echo "socket_create() failed: reason: " .socket_strerror(socket_last_error());
}
else
{
echo("socket create was successful");
echo("<br>");
}
echo ("Attempting to connect to host");
echo("<br>");
if ( ($result = socket_connect($socket, $serverAddress, $serverListeningPort)) === FALSE )
{

echo ("socket_connect() failed. Reason:".socket_strerror(socket_last_error($socket)));
}
echo ("Reading response:");
$message="";

while(true)
{
$message=socket_read($socket, 300);
if($message!=='')
{
sendMessage($message);
}
}


Code for sseReceiver.php



var evtSource=new EventSource("theSource.php");     

evtSource.onmessage=function(event)
{
var payload=JSON.parse(event.data);
// code to display payload on webpage in a table
}






php sockets






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 1:32









dollarSquaredollarSquare

133




133













  • The infinite while loop seems to be causing this issue. Is there a way to make that loop run on a different thread or in the background?

    – dollarSquare
    Nov 24 '18 at 15:17



















  • The infinite while loop seems to be causing this issue. Is there a way to make that loop run on a different thread or in the background?

    – dollarSquare
    Nov 24 '18 at 15:17

















The infinite while loop seems to be causing this issue. Is there a way to make that loop run on a different thread or in the background?

– dollarSquare
Nov 24 '18 at 15:17





The infinite while loop seems to be causing this issue. Is there a way to make that loop run on a different thread or in the background?

– dollarSquare
Nov 24 '18 at 15:17












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454432%2fhow-to-make-a-webpage-with-server-sent-event-source-using-php-sockets-not-freeze%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53454432%2fhow-to-make-a-webpage-with-server-sent-event-source-using-php-sockets-not-freeze%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Ottavio Pratesi

Tricia Helfer

15 giugno