Javascript Server Side Events: Multiple Clients Listening to Single Server Instance
I want to implement Server Side Events with multiple clients listening to a single server instance. When ever my clients connect, they create a new instance of the server. I want them all to listen to the same instance.
The following snippets describe the server, Client1 listens for "Ping" and Client2 listens for "Pong. When run, each client creates a new instance as shown by the random key generated and displayed in the broswer. My goal is to get them bot listening to the same instance. I have goggled for hours and find a lot of information and using toher libraries, but I would rather just use native. This is running in a FreePBX environemnt that includes bootstrap, jquery, socket.io and other libraries.
sse-serv.php:
<?php
date_default_timezone_set("America/New_York");
header("Content-Type: text/event-stream");
$key = rand(1111, 9999); // a random counter
$counter=0;
while ($counter < 100) {
// 1 is always true, so repeat the while loop forever (aka event-loop)
$curDate = date("Y-m-d H:i:s");
$msg=array();
$msg['count']=$counter;
$msg['time']=$curDate;
$msg['key']=$key;
$json_msg=json_encode($msg);
if ($ping) {
echo "event: pingndata: $json_msg nn";
$ping=false;
} else {
echo "event: pongndata: $json_msg nn";
$ping=true;
}
$counter++;
ob_end_flush();
flush();
if ( connection_aborted() ) break;
sleep(1);
}
?>
Every second the server alternatively sends a "Ping" or "Pong" event. The clients display exactly what I expect, excpet for the RND $Key is difference so they are clearly different instances. (Neither of which show up in ps aux | grep sse-serv for some reason).
sse-client1.php:
<?php
session_start();
require_once('include.php');
require_once('/etc/freepbx.conf');
$dt=date("Y-m-d H:i:s");
?>
<html>
<head>
<meta charset="UTF-8">
<title>Server-sent events demo</title>
</head>
<body>
<button>Close the connection</button>
<script>
var button = document.querySelector('button');
var evtSource = new EventSource('sse-serv.php');
console.log(evtSource.withCredentials);
console.log(evtSource.readyState);
console.log(evtSource.url);
var eventList = document.querySelector('ul');
evtSource.onopen = function() {
console.log("Connection to server opened.");
};
evtSource.onmessage = function(e) {
var newElement = document.createElement("li");
newElement.textContent = "message: " + e.data;
eventList.appendChild(newElement);
};
evtSource.onerror = function() {
console.log("EventSource failed.");
};
button.onclick = function() {
console.log('Connection closed');
evtSource.close();
};
evtSource.addEventListener("ping", function(e) {
var newElement = document.createElement("li");
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
var ntime= h + ":" + m + ":" + s;
var obj = JSON.parse(e.data);
newElement.innerHTML = "Key: " + obj.key + ping at " + obj.time + " Local time: " + ntime + " counter: " + obj.count;
eventList.appendChild(newElement);
}, false);
</script>
</body>
</html>
Client2 is exactly like client1 except it listens for "Pong" instead of "Ping."
When I run them both clients at once I get this:
Client1:
Key: 2642 ping at 2018-11-18 06:27:19 Local time: 6:27:19 counter: 1
Key: 2642 ping at 2018-11-18 06:27:21 Local time: 6:27:21 counter: 3
Key: 2642 ping at 2018-11-18 06:27:23 Local time: 6:27:23 counter: 5
Key: 2642 ping at 2018-11-18 06:27:25 Local time: 6:27:25 counter: 7
Key: 2642 ping at 2018-11-18 06:27:27 Local time: 6:27:27 counter: 9
Client2
Key: 1818 pong at 2018-11-18 06:27:18 Local time: 6:27:18 counter: 0
Key: 1818 pong at 2018-11-18 06:27:20 Local time: 6:27:20 counter: 2
Key: 1818 pong at 2018-11-18 06:27:22 Local time: 6:27:22 counter: 4
Key: 1818 pong at 2018-11-18 06:27:24 Local time: 6:27:24 counter: 6
Key: 1818 pong at 2018-11-18 06:27:26 Local time: 6:27:26 counter: 8
This output is exactly what I want, except the different keys prove each client is running a seperate instance of sse-serv.php. I need them to both listen to an existing instance of sse-serv.php which will run as a daemon and broadcast to all clients. How can I do that?
javascript server-sent-events
add a comment |
I want to implement Server Side Events with multiple clients listening to a single server instance. When ever my clients connect, they create a new instance of the server. I want them all to listen to the same instance.
The following snippets describe the server, Client1 listens for "Ping" and Client2 listens for "Pong. When run, each client creates a new instance as shown by the random key generated and displayed in the broswer. My goal is to get them bot listening to the same instance. I have goggled for hours and find a lot of information and using toher libraries, but I would rather just use native. This is running in a FreePBX environemnt that includes bootstrap, jquery, socket.io and other libraries.
sse-serv.php:
<?php
date_default_timezone_set("America/New_York");
header("Content-Type: text/event-stream");
$key = rand(1111, 9999); // a random counter
$counter=0;
while ($counter < 100) {
// 1 is always true, so repeat the while loop forever (aka event-loop)
$curDate = date("Y-m-d H:i:s");
$msg=array();
$msg['count']=$counter;
$msg['time']=$curDate;
$msg['key']=$key;
$json_msg=json_encode($msg);
if ($ping) {
echo "event: pingndata: $json_msg nn";
$ping=false;
} else {
echo "event: pongndata: $json_msg nn";
$ping=true;
}
$counter++;
ob_end_flush();
flush();
if ( connection_aborted() ) break;
sleep(1);
}
?>
Every second the server alternatively sends a "Ping" or "Pong" event. The clients display exactly what I expect, excpet for the RND $Key is difference so they are clearly different instances. (Neither of which show up in ps aux | grep sse-serv for some reason).
sse-client1.php:
<?php
session_start();
require_once('include.php');
require_once('/etc/freepbx.conf');
$dt=date("Y-m-d H:i:s");
?>
<html>
<head>
<meta charset="UTF-8">
<title>Server-sent events demo</title>
</head>
<body>
<button>Close the connection</button>
<script>
var button = document.querySelector('button');
var evtSource = new EventSource('sse-serv.php');
console.log(evtSource.withCredentials);
console.log(evtSource.readyState);
console.log(evtSource.url);
var eventList = document.querySelector('ul');
evtSource.onopen = function() {
console.log("Connection to server opened.");
};
evtSource.onmessage = function(e) {
var newElement = document.createElement("li");
newElement.textContent = "message: " + e.data;
eventList.appendChild(newElement);
};
evtSource.onerror = function() {
console.log("EventSource failed.");
};
button.onclick = function() {
console.log('Connection closed');
evtSource.close();
};
evtSource.addEventListener("ping", function(e) {
var newElement = document.createElement("li");
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
var ntime= h + ":" + m + ":" + s;
var obj = JSON.parse(e.data);
newElement.innerHTML = "Key: " + obj.key + ping at " + obj.time + " Local time: " + ntime + " counter: " + obj.count;
eventList.appendChild(newElement);
}, false);
</script>
</body>
</html>
Client2 is exactly like client1 except it listens for "Pong" instead of "Ping."
When I run them both clients at once I get this:
Client1:
Key: 2642 ping at 2018-11-18 06:27:19 Local time: 6:27:19 counter: 1
Key: 2642 ping at 2018-11-18 06:27:21 Local time: 6:27:21 counter: 3
Key: 2642 ping at 2018-11-18 06:27:23 Local time: 6:27:23 counter: 5
Key: 2642 ping at 2018-11-18 06:27:25 Local time: 6:27:25 counter: 7
Key: 2642 ping at 2018-11-18 06:27:27 Local time: 6:27:27 counter: 9
Client2
Key: 1818 pong at 2018-11-18 06:27:18 Local time: 6:27:18 counter: 0
Key: 1818 pong at 2018-11-18 06:27:20 Local time: 6:27:20 counter: 2
Key: 1818 pong at 2018-11-18 06:27:22 Local time: 6:27:22 counter: 4
Key: 1818 pong at 2018-11-18 06:27:24 Local time: 6:27:24 counter: 6
Key: 1818 pong at 2018-11-18 06:27:26 Local time: 6:27:26 counter: 8
This output is exactly what I want, except the different keys prove each client is running a seperate instance of sse-serv.php. I need them to both listen to an existing instance of sse-serv.php which will run as a daemon and broadcast to all clients. How can I do that?
javascript server-sent-events
add a comment |
I want to implement Server Side Events with multiple clients listening to a single server instance. When ever my clients connect, they create a new instance of the server. I want them all to listen to the same instance.
The following snippets describe the server, Client1 listens for "Ping" and Client2 listens for "Pong. When run, each client creates a new instance as shown by the random key generated and displayed in the broswer. My goal is to get them bot listening to the same instance. I have goggled for hours and find a lot of information and using toher libraries, but I would rather just use native. This is running in a FreePBX environemnt that includes bootstrap, jquery, socket.io and other libraries.
sse-serv.php:
<?php
date_default_timezone_set("America/New_York");
header("Content-Type: text/event-stream");
$key = rand(1111, 9999); // a random counter
$counter=0;
while ($counter < 100) {
// 1 is always true, so repeat the while loop forever (aka event-loop)
$curDate = date("Y-m-d H:i:s");
$msg=array();
$msg['count']=$counter;
$msg['time']=$curDate;
$msg['key']=$key;
$json_msg=json_encode($msg);
if ($ping) {
echo "event: pingndata: $json_msg nn";
$ping=false;
} else {
echo "event: pongndata: $json_msg nn";
$ping=true;
}
$counter++;
ob_end_flush();
flush();
if ( connection_aborted() ) break;
sleep(1);
}
?>
Every second the server alternatively sends a "Ping" or "Pong" event. The clients display exactly what I expect, excpet for the RND $Key is difference so they are clearly different instances. (Neither of which show up in ps aux | grep sse-serv for some reason).
sse-client1.php:
<?php
session_start();
require_once('include.php');
require_once('/etc/freepbx.conf');
$dt=date("Y-m-d H:i:s");
?>
<html>
<head>
<meta charset="UTF-8">
<title>Server-sent events demo</title>
</head>
<body>
<button>Close the connection</button>
<script>
var button = document.querySelector('button');
var evtSource = new EventSource('sse-serv.php');
console.log(evtSource.withCredentials);
console.log(evtSource.readyState);
console.log(evtSource.url);
var eventList = document.querySelector('ul');
evtSource.onopen = function() {
console.log("Connection to server opened.");
};
evtSource.onmessage = function(e) {
var newElement = document.createElement("li");
newElement.textContent = "message: " + e.data;
eventList.appendChild(newElement);
};
evtSource.onerror = function() {
console.log("EventSource failed.");
};
button.onclick = function() {
console.log('Connection closed');
evtSource.close();
};
evtSource.addEventListener("ping", function(e) {
var newElement = document.createElement("li");
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
var ntime= h + ":" + m + ":" + s;
var obj = JSON.parse(e.data);
newElement.innerHTML = "Key: " + obj.key + ping at " + obj.time + " Local time: " + ntime + " counter: " + obj.count;
eventList.appendChild(newElement);
}, false);
</script>
</body>
</html>
Client2 is exactly like client1 except it listens for "Pong" instead of "Ping."
When I run them both clients at once I get this:
Client1:
Key: 2642 ping at 2018-11-18 06:27:19 Local time: 6:27:19 counter: 1
Key: 2642 ping at 2018-11-18 06:27:21 Local time: 6:27:21 counter: 3
Key: 2642 ping at 2018-11-18 06:27:23 Local time: 6:27:23 counter: 5
Key: 2642 ping at 2018-11-18 06:27:25 Local time: 6:27:25 counter: 7
Key: 2642 ping at 2018-11-18 06:27:27 Local time: 6:27:27 counter: 9
Client2
Key: 1818 pong at 2018-11-18 06:27:18 Local time: 6:27:18 counter: 0
Key: 1818 pong at 2018-11-18 06:27:20 Local time: 6:27:20 counter: 2
Key: 1818 pong at 2018-11-18 06:27:22 Local time: 6:27:22 counter: 4
Key: 1818 pong at 2018-11-18 06:27:24 Local time: 6:27:24 counter: 6
Key: 1818 pong at 2018-11-18 06:27:26 Local time: 6:27:26 counter: 8
This output is exactly what I want, except the different keys prove each client is running a seperate instance of sse-serv.php. I need them to both listen to an existing instance of sse-serv.php which will run as a daemon and broadcast to all clients. How can I do that?
javascript server-sent-events
I want to implement Server Side Events with multiple clients listening to a single server instance. When ever my clients connect, they create a new instance of the server. I want them all to listen to the same instance.
The following snippets describe the server, Client1 listens for "Ping" and Client2 listens for "Pong. When run, each client creates a new instance as shown by the random key generated and displayed in the broswer. My goal is to get them bot listening to the same instance. I have goggled for hours and find a lot of information and using toher libraries, but I would rather just use native. This is running in a FreePBX environemnt that includes bootstrap, jquery, socket.io and other libraries.
sse-serv.php:
<?php
date_default_timezone_set("America/New_York");
header("Content-Type: text/event-stream");
$key = rand(1111, 9999); // a random counter
$counter=0;
while ($counter < 100) {
// 1 is always true, so repeat the while loop forever (aka event-loop)
$curDate = date("Y-m-d H:i:s");
$msg=array();
$msg['count']=$counter;
$msg['time']=$curDate;
$msg['key']=$key;
$json_msg=json_encode($msg);
if ($ping) {
echo "event: pingndata: $json_msg nn";
$ping=false;
} else {
echo "event: pongndata: $json_msg nn";
$ping=true;
}
$counter++;
ob_end_flush();
flush();
if ( connection_aborted() ) break;
sleep(1);
}
?>
Every second the server alternatively sends a "Ping" or "Pong" event. The clients display exactly what I expect, excpet for the RND $Key is difference so they are clearly different instances. (Neither of which show up in ps aux | grep sse-serv for some reason).
sse-client1.php:
<?php
session_start();
require_once('include.php');
require_once('/etc/freepbx.conf');
$dt=date("Y-m-d H:i:s");
?>
<html>
<head>
<meta charset="UTF-8">
<title>Server-sent events demo</title>
</head>
<body>
<button>Close the connection</button>
<script>
var button = document.querySelector('button');
var evtSource = new EventSource('sse-serv.php');
console.log(evtSource.withCredentials);
console.log(evtSource.readyState);
console.log(evtSource.url);
var eventList = document.querySelector('ul');
evtSource.onopen = function() {
console.log("Connection to server opened.");
};
evtSource.onmessage = function(e) {
var newElement = document.createElement("li");
newElement.textContent = "message: " + e.data;
eventList.appendChild(newElement);
};
evtSource.onerror = function() {
console.log("EventSource failed.");
};
button.onclick = function() {
console.log('Connection closed');
evtSource.close();
};
evtSource.addEventListener("ping", function(e) {
var newElement = document.createElement("li");
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
var ntime= h + ":" + m + ":" + s;
var obj = JSON.parse(e.data);
newElement.innerHTML = "Key: " + obj.key + ping at " + obj.time + " Local time: " + ntime + " counter: " + obj.count;
eventList.appendChild(newElement);
}, false);
</script>
</body>
</html>
Client2 is exactly like client1 except it listens for "Pong" instead of "Ping."
When I run them both clients at once I get this:
Client1:
Key: 2642 ping at 2018-11-18 06:27:19 Local time: 6:27:19 counter: 1
Key: 2642 ping at 2018-11-18 06:27:21 Local time: 6:27:21 counter: 3
Key: 2642 ping at 2018-11-18 06:27:23 Local time: 6:27:23 counter: 5
Key: 2642 ping at 2018-11-18 06:27:25 Local time: 6:27:25 counter: 7
Key: 2642 ping at 2018-11-18 06:27:27 Local time: 6:27:27 counter: 9
Client2
Key: 1818 pong at 2018-11-18 06:27:18 Local time: 6:27:18 counter: 0
Key: 1818 pong at 2018-11-18 06:27:20 Local time: 6:27:20 counter: 2
Key: 1818 pong at 2018-11-18 06:27:22 Local time: 6:27:22 counter: 4
Key: 1818 pong at 2018-11-18 06:27:24 Local time: 6:27:24 counter: 6
Key: 1818 pong at 2018-11-18 06:27:26 Local time: 6:27:26 counter: 8
This output is exactly what I want, except the different keys prove each client is running a seperate instance of sse-serv.php. I need them to both listen to an existing instance of sse-serv.php which will run as a daemon and broadcast to all clients. How can I do that?
javascript server-sent-events
javascript server-sent-events
asked Nov 18 '18 at 16:15
jerryrigjerryrig
558
558
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
EDIT: I've skipped a lot of detail in this answer. I mention WebSockets here because eventually you are likely to discover you need more feature/flexibility support than Server-Sent Events provides.
You will have better success with a WebSocket server in PHP. You've simply created a long running process that is invoked by your web server as standard PHP MIME type. This means that for each request to the PHP endpoint (as you recognize) you have spun up a new instance of PHP. Most likely, your URL to the PHP page uses http://
...
A web socket server URL starts with ws://
. This tells the server to use the single existing running process. The web socket server then tracks all the "channels" available and broadcasts to those channels.
The quickest fix for this is for you to use the Ratchet WSS found here: http://socketo.me
That should fix you up. If you prefer to write your own, there are plenty of resources available to you.
add a comment |
Every PHP script is completely isolated by default. If you're just looking to quickly prototype this, I would suggest using a different environment. In node.js everything is shared and re-doing your example would be trivial.
If PHP is a requirement and you're willing to put the extra time in, don't make PHP handle multiple connections, but instead offload the 'pub/sub' aspect of your application to dedicated server. There are lots of pub/sub-like systems out there with good PHP clients. Each with varying scalability properties and complexity. Redis is fairly easy to get started with.
Thanks, Evert. Node.js is already installed in this FreeBPX distro, Can you point me at some examples that would allow me to accomplsih the above with Node.js? I looked at WebSockets, but it seemed more overhead as bi-directional and I don't need that at all.
– jerryrig
Nov 18 '18 at 23:24
@jerryrig when I need to solve a problem like this, I start with google and the manual just like you. I don't have a library of solutions to common problems. If you're stuck with something, I would suggest you ask a new question here!
– Evert
Nov 19 '18 at 4:30
add a comment |
This was exactly what I needed. So now I have my own server written in PHP and client libraries, that can handle
connections via ws:// wss:// tcp:// ssl://
I am using this one currrently in a web application within our intranet.
Have a look at https://github.com/napengam/phpWebSocketServer
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53362935%2fjavascript-server-side-events-multiple-clients-listening-to-single-server-insta%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
EDIT: I've skipped a lot of detail in this answer. I mention WebSockets here because eventually you are likely to discover you need more feature/flexibility support than Server-Sent Events provides.
You will have better success with a WebSocket server in PHP. You've simply created a long running process that is invoked by your web server as standard PHP MIME type. This means that for each request to the PHP endpoint (as you recognize) you have spun up a new instance of PHP. Most likely, your URL to the PHP page uses http://
...
A web socket server URL starts with ws://
. This tells the server to use the single existing running process. The web socket server then tracks all the "channels" available and broadcasts to those channels.
The quickest fix for this is for you to use the Ratchet WSS found here: http://socketo.me
That should fix you up. If you prefer to write your own, there are plenty of resources available to you.
add a comment |
EDIT: I've skipped a lot of detail in this answer. I mention WebSockets here because eventually you are likely to discover you need more feature/flexibility support than Server-Sent Events provides.
You will have better success with a WebSocket server in PHP. You've simply created a long running process that is invoked by your web server as standard PHP MIME type. This means that for each request to the PHP endpoint (as you recognize) you have spun up a new instance of PHP. Most likely, your URL to the PHP page uses http://
...
A web socket server URL starts with ws://
. This tells the server to use the single existing running process. The web socket server then tracks all the "channels" available and broadcasts to those channels.
The quickest fix for this is for you to use the Ratchet WSS found here: http://socketo.me
That should fix you up. If you prefer to write your own, there are plenty of resources available to you.
add a comment |
EDIT: I've skipped a lot of detail in this answer. I mention WebSockets here because eventually you are likely to discover you need more feature/flexibility support than Server-Sent Events provides.
You will have better success with a WebSocket server in PHP. You've simply created a long running process that is invoked by your web server as standard PHP MIME type. This means that for each request to the PHP endpoint (as you recognize) you have spun up a new instance of PHP. Most likely, your URL to the PHP page uses http://
...
A web socket server URL starts with ws://
. This tells the server to use the single existing running process. The web socket server then tracks all the "channels" available and broadcasts to those channels.
The quickest fix for this is for you to use the Ratchet WSS found here: http://socketo.me
That should fix you up. If you prefer to write your own, there are plenty of resources available to you.
EDIT: I've skipped a lot of detail in this answer. I mention WebSockets here because eventually you are likely to discover you need more feature/flexibility support than Server-Sent Events provides.
You will have better success with a WebSocket server in PHP. You've simply created a long running process that is invoked by your web server as standard PHP MIME type. This means that for each request to the PHP endpoint (as you recognize) you have spun up a new instance of PHP. Most likely, your URL to the PHP page uses http://
...
A web socket server URL starts with ws://
. This tells the server to use the single existing running process. The web socket server then tracks all the "channels" available and broadcasts to those channels.
The quickest fix for this is for you to use the Ratchet WSS found here: http://socketo.me
That should fix you up. If you prefer to write your own, there are plenty of resources available to you.
edited Nov 18 '18 at 16:48
answered Nov 18 '18 at 16:32
Randy CasburnRandy Casburn
4,9741318
4,9741318
add a comment |
add a comment |
Every PHP script is completely isolated by default. If you're just looking to quickly prototype this, I would suggest using a different environment. In node.js everything is shared and re-doing your example would be trivial.
If PHP is a requirement and you're willing to put the extra time in, don't make PHP handle multiple connections, but instead offload the 'pub/sub' aspect of your application to dedicated server. There are lots of pub/sub-like systems out there with good PHP clients. Each with varying scalability properties and complexity. Redis is fairly easy to get started with.
Thanks, Evert. Node.js is already installed in this FreeBPX distro, Can you point me at some examples that would allow me to accomplsih the above with Node.js? I looked at WebSockets, but it seemed more overhead as bi-directional and I don't need that at all.
– jerryrig
Nov 18 '18 at 23:24
@jerryrig when I need to solve a problem like this, I start with google and the manual just like you. I don't have a library of solutions to common problems. If you're stuck with something, I would suggest you ask a new question here!
– Evert
Nov 19 '18 at 4:30
add a comment |
Every PHP script is completely isolated by default. If you're just looking to quickly prototype this, I would suggest using a different environment. In node.js everything is shared and re-doing your example would be trivial.
If PHP is a requirement and you're willing to put the extra time in, don't make PHP handle multiple connections, but instead offload the 'pub/sub' aspect of your application to dedicated server. There are lots of pub/sub-like systems out there with good PHP clients. Each with varying scalability properties and complexity. Redis is fairly easy to get started with.
Thanks, Evert. Node.js is already installed in this FreeBPX distro, Can you point me at some examples that would allow me to accomplsih the above with Node.js? I looked at WebSockets, but it seemed more overhead as bi-directional and I don't need that at all.
– jerryrig
Nov 18 '18 at 23:24
@jerryrig when I need to solve a problem like this, I start with google and the manual just like you. I don't have a library of solutions to common problems. If you're stuck with something, I would suggest you ask a new question here!
– Evert
Nov 19 '18 at 4:30
add a comment |
Every PHP script is completely isolated by default. If you're just looking to quickly prototype this, I would suggest using a different environment. In node.js everything is shared and re-doing your example would be trivial.
If PHP is a requirement and you're willing to put the extra time in, don't make PHP handle multiple connections, but instead offload the 'pub/sub' aspect of your application to dedicated server. There are lots of pub/sub-like systems out there with good PHP clients. Each with varying scalability properties and complexity. Redis is fairly easy to get started with.
Every PHP script is completely isolated by default. If you're just looking to quickly prototype this, I would suggest using a different environment. In node.js everything is shared and re-doing your example would be trivial.
If PHP is a requirement and you're willing to put the extra time in, don't make PHP handle multiple connections, but instead offload the 'pub/sub' aspect of your application to dedicated server. There are lots of pub/sub-like systems out there with good PHP clients. Each with varying scalability properties and complexity. Redis is fairly easy to get started with.
answered Nov 18 '18 at 16:53
EvertEvert
41.2k1570125
41.2k1570125
Thanks, Evert. Node.js is already installed in this FreeBPX distro, Can you point me at some examples that would allow me to accomplsih the above with Node.js? I looked at WebSockets, but it seemed more overhead as bi-directional and I don't need that at all.
– jerryrig
Nov 18 '18 at 23:24
@jerryrig when I need to solve a problem like this, I start with google and the manual just like you. I don't have a library of solutions to common problems. If you're stuck with something, I would suggest you ask a new question here!
– Evert
Nov 19 '18 at 4:30
add a comment |
Thanks, Evert. Node.js is already installed in this FreeBPX distro, Can you point me at some examples that would allow me to accomplsih the above with Node.js? I looked at WebSockets, but it seemed more overhead as bi-directional and I don't need that at all.
– jerryrig
Nov 18 '18 at 23:24
@jerryrig when I need to solve a problem like this, I start with google and the manual just like you. I don't have a library of solutions to common problems. If you're stuck with something, I would suggest you ask a new question here!
– Evert
Nov 19 '18 at 4:30
Thanks, Evert. Node.js is already installed in this FreeBPX distro, Can you point me at some examples that would allow me to accomplsih the above with Node.js? I looked at WebSockets, but it seemed more overhead as bi-directional and I don't need that at all.
– jerryrig
Nov 18 '18 at 23:24
Thanks, Evert. Node.js is already installed in this FreeBPX distro, Can you point me at some examples that would allow me to accomplsih the above with Node.js? I looked at WebSockets, but it seemed more overhead as bi-directional and I don't need that at all.
– jerryrig
Nov 18 '18 at 23:24
@jerryrig when I need to solve a problem like this, I start with google and the manual just like you. I don't have a library of solutions to common problems. If you're stuck with something, I would suggest you ask a new question here!
– Evert
Nov 19 '18 at 4:30
@jerryrig when I need to solve a problem like this, I start with google and the manual just like you. I don't have a library of solutions to common problems. If you're stuck with something, I would suggest you ask a new question here!
– Evert
Nov 19 '18 at 4:30
add a comment |
This was exactly what I needed. So now I have my own server written in PHP and client libraries, that can handle
connections via ws:// wss:// tcp:// ssl://
I am using this one currrently in a web application within our intranet.
Have a look at https://github.com/napengam/phpWebSocketServer
add a comment |
This was exactly what I needed. So now I have my own server written in PHP and client libraries, that can handle
connections via ws:// wss:// tcp:// ssl://
I am using this one currrently in a web application within our intranet.
Have a look at https://github.com/napengam/phpWebSocketServer
add a comment |
This was exactly what I needed. So now I have my own server written in PHP and client libraries, that can handle
connections via ws:// wss:// tcp:// ssl://
I am using this one currrently in a web application within our intranet.
Have a look at https://github.com/napengam/phpWebSocketServer
This was exactly what I needed. So now I have my own server written in PHP and client libraries, that can handle
connections via ws:// wss:// tcp:// ssl://
I am using this one currrently in a web application within our intranet.
Have a look at https://github.com/napengam/phpWebSocketServer
answered Nov 22 '18 at 15:58
HeinzHeinz
112
112
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53362935%2fjavascript-server-side-events-multiple-clients-listening-to-single-server-insta%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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