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

99封情书 提交于 2020-02-27 08:10:18

问题


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.


回答1:


If your shared hosting provides PHP support, you can use one of the WebSockets libraries in PHP:

  • Ratchet
  • phpwebsocket
  • PHP-WebSockets

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.




回答2:


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



来源:https://stackoverflow.com/questions/20932466/is-there-any-alternative-to-the-websockets-for-using-in-shared-hosting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!