Is there any alternative for the websockets to use in shared hosting? I know about node.js, socket.io, Express.js but Can't use them in shared hosting. So, if there is any alternative used for making a realtime website then tell me.
If your shared hosting provides PHP support, you can use one of the WebSockets libraries in PHP:
For installing Ratchet, read my answer on how to install Composer on a shared hosting.
Alternatively, you can install Node.js on a shared hosting using my project Node.php.
ssh
access you can use websocketd easy to use, quick to implement and free –
Ailing I think good alternative is "Server-Sent Events", it is one way but i think its in the most cases better that Websockets because its easier to setup as no special server or libraries needed and there is no extra protocol to follow, just echo
from php
and onmessage
in javascript
.
quick example (from https://www.w3schools.com/html/html5_serversentevents.asp):
Javascript:
var source = new EventSource("demo_sse.php");
source.onmessage = function(event) {
document.getElementById("result").innerHTML += event.data + "<br>";
};
PHP:
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$time = date('r');
echo "data: The server time is: {$time}\n\n";
flush();
?>
You may consider using a hosted realtime network like PubNub for realtime communication on shared hosting. Using a hosted realtime solution like PubNub means you won't need to worry about open ports or persistent processes.
There is a full hello world tutorial to help you get started on the PubNub blog here: http://www.pubnub.com/blog/php-push-api-walkthrough/
A simple example follows.
Let’s take a look at how developers can create channels between PHP and JavaScript. The most common usage pattern for real time applications will be explained first. A JavaScript Browser (like Firefox) will subscribe and listen for messages with PUBNUB.subscribe(). PHP will then push messages with $pubnub.publish().
PUBNUB.subscribe( { channel : 'my_test_channel' }, function(message) { if ('some_text' in message) { alert(message.some_text); }} );
The above JavaScript is fully cross browser compatible. The code will listen for messages published on ‘my_test_channel’ channel. When a message is received, the JavaScript will validate if ‘some_text‘ exists in the message object. If this attribute exists, then show an alert box!
Now use PHP to publish a message to invoke the JavaScript Alert box.
## Publish Messages To a JavaScript Browser $pubnub = new Pubnub( 'publish_key', 'subscribe_key' ); $pubnub->publish(array( 'channel' => 'my_test_channel', 'message' => array( 'some_text' => 'hello!' ) ));
This PHP code will send a message to a JavaScript Browser listening on ‘my_test_channel‘ channel. When this PHP Code executes, a JavaScript Browser will receive the PHP array and show an alert message of ‘hello!’.
http://www.pubnub.com/blog/php-push-api-walkthrough/#sthash.jI8zntnL.dpuf
If your shared hosting provides PHP support, you can use one of the WebSockets libraries in PHP:
For installing Ratchet, read my answer on how to install Composer on a shared hosting.
Alternatively, you can install Node.js on a shared hosting using my project Node.php.
ssh
access you can use websocketd easy to use, quick to implement and free –
Ailing © 2022 - 2024 — McMap. All rights reserved.