Is there any alternative to the websockets for using in shared hosting
Asked Answered
C

3

7

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.

Catastrophism answered 5/1, 2014 at 10:26 Comment(3)
Your best bet would be to get a VPS instead, they go for as low as $5/moNanji
Get a VPS. Shared hosting sites usually have certain limitations due to their internal server organization. #17530113Schoolmaster
@Ineentho Did you mean a web server hosting from office or home? if it is then it is very difficult for me that I'm in Pakistan and there is load shedding of electricity. So, Please tell me is there any other thing to use in?Catastrophism
B
3

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.

Bazan answered 28/4, 2015 at 10:24 Comment(3)
does it mean that rachet can run in a shared hosting accountFro
The options that you have suggested is based on a stand alone serverMooney
if you have ssh access you can use websocketd easy to use, quick to implement and freeAiling
A
7

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();
?>
Ailing answered 4/6, 2020 at 23:26 Comment(3)
IMO this is really the best answer. The suggestions made in the accepted answer doesn't look to fulfill the needs of the OP. SSE is really the way to go on this.Numeration
Server sent events don't really work well with apache thoughPasadena
On my shared hosting the server doesn't send any messages. In localhost it works. How can I debug it?Noe
P
3

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.

enter image description here

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

Pyelography answered 9/1, 2014 at 21:10 Comment(0)
B
3

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.

Bazan answered 28/4, 2015 at 10:24 Comment(3)
does it mean that rachet can run in a shared hosting accountFro
The options that you have suggested is based on a stand alone serverMooney
if you have ssh access you can use websocketd easy to use, quick to implement and freeAiling

© 2022 - 2024 — McMap. All rights reserved.