I'm trying to set up PHP websockets on my website. This works great on my local WAMP server, but on my website I keep getting a warning:
unable to bind address [98]: Address already in use in"
I tried various libraries, but they all return this error.
My guess is that the port I'm using in isn't free. The problem is, that I cannot access terminal since this is a shared server (according to phpinfo() websockets are enabled, btw) so I can't look for free ports. Also tried to use port 0 - but no luck.
Thanks in advance!
EDIT:
For instance, this is some code using https://github.com/Flynsarmy/PHPWebSocket-Chat
// start the server
$Server = new PHPWebSocket();
$Server->bind('message', 'wsOnMessage');
$Server->bind('open', 'wsOnOpen');
$Server->bind('close', 'wsOnClose');
// for other computers to connect, you will probably need to change this to your LAN IP or external IP,
// alternatively use: gethostbyaddr(gethostbyname($_SERVER['SERVER_NAME']))
$Server->wsStartServer($_SERVER['SERVER_ADDR'], 9300);
Well, Bluehost site writes: "We block access to certain ports to help avoid having security holes in the firewall...Purchasing a dedicated IP will allow us to grant you access to the ports you will need to run your specific services on.". The technical support guy told me otherwise. I guess case is closed. Thank you all for your time!
talhof9 I went through similar pain in trying to configure my shared hosting service, I didn't find a direct solution to get a shared *AMP server to support WebSockets, but I found a workaround that will at least let you test the commercial viability of the solution you are putting together (if that is indeed what you are looking for) without paying for all the headache of setting up,configuring and administering your own VPS.
Check out http://www.pusher.com for an easy websocket deployment library, that uses their Node server. The free sandbox version lets you play around to get it working, and once you want to test commercial viability you can upgrade to a paid plan.
Hope this helps!
(note I do not work for Pusher)
Most probably your hosting provider has, somehow, disabled PHP sockets. This makes sense because PHP is used to process webpages not create daemons and you're probably using a regular web hosting plan (not a dedicated server).
I would check in with your hosting provider - support forum or just call them.
Related
I want to run a basic chat application on my shared hosting.
I would use a PHP Websockets implementation library, Ratchet.
However, when I go to my shared hosting (Hostgator) websockets information page, it stated:
PHP Socket Support?
If you are connecting out, it should work. We do not allow clients to bind to local ports for incoming.
What does it mean? Can I create my own websocket running the command via ssh? I would use this basic code in order to run it.
require dirname(__DIR__) . '/vendor/autoload.php';
$server = IoServer::factory(
new HttpServer(
new WsServer(
new Chat()
)
),
8081
);
$server->run();
I noticed there were similar questions but most of the answers were saying it's not possible because the questioner was trying to use Node.js or Python websockets libraries, which are not supported on most of the shared hosting.
Main answer
Shared hosting will generally allow you to listen on a high port, e.g. the one you are using. However, there will be a number of problems in practice.
Firstly, the web server will probably only allow 80 (HTTP) and 443 (HTTPS) inbound, so a port of 8081 would be blocked by the firewall. Your PHP listener would attach to the port, but would wait patiently for traffic that never comes.
Secondly, some shared hosts will have a load balancer in front of them, and they might only be configured to forward HTTP traffic. Since Web Sockets are a different protocol, they won't be set up to forward that. The same problem with ports is repeated here too - non-standard ports won't be forwarded.
To solve these problems, you need your own web server, where you can open ports (and set up load balancers) in whatever way you like. This is pretty cheap to do these days - for the price of a couple of cafe coffees per month, you can rent a small virtual server. It won't have as much RAM as a shared server, but it will be a lot more flexible.
Design issues
I would draw attention also to using non-standard ports for Web Sockets to service a web application on standard 80/443 ports. This is not always a good idea. The non-standard ports will work fine on desktops and standard home internet connections, but for some office or mobile internet connections, you might get into a pickle.
It is better to put a load balancer in front of your app and then let it route traffic (Web Socket or HTTP) based on the protocol signature. This will allow you to use multiple protocols per port. If you are interested in exploring this, I recommend Traefik with Docker containers - I have set this up and it works very well indeed.
I have a shared hosting plan (linux).
i followed a lot of guides and questions guides:
setting up websockets without command line
https://www.sitepoint.com/how-to-quickly-build-a-chat-app-with-ratchet/
and some others about connecting to SSH using Putty, phpShell, etc, etc...
A really lot of guides.
But nothing worked.
So, i am wondering if there is a way to know if my hosting service is blocking websockets or ssh connection or whatever i can use.
Why don't you use c panel as they restrict such services due to shared hosting setup.
I am using a shared hosting in which i can't use node js.
But I want to create a real time application using laravel as per there https://laravel.com/docs/5.3/broadcasting doc they provide drivers they all uses node if I am not wrong.
I have also searched about https://github.com/Askedio/laravel-ratchet which works with laravel but it requires a ZMQ which I can't install on my shared hosting.
What should I do any ideas.
You could use Pusher and combine it with Laravel Echo. Echo is a front-end JS framework, so just include the assets and you should be fine.
I think it should be said that it's probably not a good idea to develop these kinds of apps on shared hosted servers, due to a couple of reasons:
You almost never have SSH access, so deploying and debugging is an immense pain
You can't install additional software you need, like NodeJS
Shared hosted servers often aren't build to run heavy php applications, they are build to serve simple websites (that's why they're so cheap)
Depending on your host, all other ports than 80 and 443 are closed, so usage of external services can be blocked
All in all, I think it's better to just get a cheap VPS (i.e. a DigitalOcean $10 plan) in which you can actually do all those things. Yes it's more expensive than shared hosting, but if that's really the game-changer, find a sponsor or partner. $120 is a piece of cake compared to the pains of using shared hosting, and that's speaking out of experience.
This may sound like a dumb question, but I'm new to this. I'm setting up a website, and I want it to be able to have access to a MySQL database. I've recently downloaded MySQL 5.6 and have used the Workbench and Command Line Client to learn to make databases.
But all of this seems to be running of localhost, so I think it's just running off my computer, and when I shut it down the website won't be able to access it.
Is this correct?
Do I have to run it somewhere else to keep it accessible to my website 24/7, even when my computer is off?
Well, yes it is running of localhost unless you deployed it on another server. Yes, you have to run it somewhere else if you want it to run when your PC is turned off. In fact, if you want to make a website and use a database for it then there are many services out there that provide free hosting and come with a MySQL database.
An example would be freehosting.com without intending to do advertisement. A quick research would show you many alternatives.
Currently its running on your localhost or your computer. To make accessible website 24/7 to need to using hosting services like, Godday, Hostgator, bluehost or there hosting.
You can purchage you domain at very low cost with full services on Godday with full support.
In computer networking, localhost is a hostname that means this computer and may be used to access the computer's own network services via its loopback network interface.
Source: https://en.wikipedia.org/wiki/Localhost
In other words: every computer is its own "localhost", not just yours, and generally MySql and other database engines use localhost as default value.
See also https://dev.mysql.com/doc/refman/5.0/en/connecting.html
If you want to have your database accessible from the net without to use an external hosting service you have to keep your computer running 24/7. Don't forget to buy a good UPS (uninterruptible power supply).
A few days ago I setup this WebSocket server from http://code.google.com/p/phpwebsocket/
It works excellent on my localhost by using Xampp. Then, I uploaded it to my webspace on Strato, but now I am not able to connect to the server.
I changed the the sockets connection to my domain but it didn`t work
Client.html:
var host = "ws://xxxxxxxx.com:12345/Websocket/server.php";
Server.php:
$master = WebSocket("xxxxxx",12345);
I already tried it with different port(80,443,12345,8080,8000....), using the IP address in server.php instead of the domain.
I also used absolute and relative paths in the client.html.
From the projects page http://code.google.com/p/phpwebsocket/ someone suggested to use 0 or 0.0.0.0 in the server.php, but that didn`t work either.
In the client.html it just shows:
WebSocket - status 0
Disconnected - status 3
My guess that the problem is that Strato is blocks me from using WebSockets.
Is it possible to run WebSockets on my webspace?
My bet is that your host is blocking incoming requests to the socket. Your not supposed to run scripts like phpwebsocket within a web service (it is a server itself). This confuses a lot of people since PHP is typically used for scripting webpages, not for coding daemons.
I say this because you mentioned running the script within Xampp, and referred to your hosting service as 'webspace'.
If this is the case you will have to upgrade to a virtual server package so that you can run your own services.
Okay, to bring this to an end.
I found a solution to use my websockets app and I´ve got an explanation why it did not work on my shared hosting solution.
I contacted Strato who told me that they don`t allow Websocket on shared hosting. The only way to get a similiar result would be to use AJAX Long Pooling.
Now I purchased to a Virtual Server and my websocket app works great now.
Thanks for your support.