I am new to web-sockets, What I wanted to achieve is run some bash script on my linux pc that is behind the NAT, I have a server running PHP, I was thinking to use websockets, I want to run a python websocket client which listens to my PHP server and run some bash scripts on certain events. I am not sure if this is possible or makes any sense at all. Hope somebody will point me to right direction??
No, this doesn't make sense.
Javascript applications running in web browsers can make a connection to a WebSocket server, which runs as part of a web server. While it is possible (if difficult) to connect to a WebSocket server using a non-browser-based client, doing so has limited utility.
If what you want to do is launch shell scripts from a web server running PHP, you don't need sockets at all, Web- or otherwise. Execute the scripts using shell_exec() or proc_open().
I found a solution, I am using the python client from https://pypi.python.org/pypi/websocket-client/ for the client machine and using Ratchet http://socketo.me/ for my LAMP server, my client listens to the message from server and runs the script and sends back the output using same socket (OR open up the reverse ssh connection to my server and I can SSH to my client machine)
Related
Is there a way for PHP to send a signal to a Windows Server to run a script?
I am writing a web application which generates database entries that are later synced by a program on a remote Windows Server. PHP & mySQL are running on AWS.
The application on the windows server syncs the mySql database with Quickbooks. Ideally I would like to send a signal from PHP so that if PHP has updated the database, letting the remote Windows server know which script to run to in order to initiate the sync program. I would need to be pointed in the right direction both on the PHP commands as well as how to setup the listening service on Windows.
I was thinking if I could get Windows to listen on a specific port for a short XML file, PHP could send a password and entity ID number to identify which script to run.
Any suggestions?
Thanks!
You can use php on both end. Just install php server on windows, then when request happened execute a windows command. Php can do that.
I would schedule a task to read a specific file from the windows server side and from the php side you could use cron jobs. This question is too broad though and there are several approaches to that. You need to be more "code" specific.
I was wondering if its possible create a website combined with NodeJs, Websocket and php in the same Apache server. Is there any documentation that can i read?
"In the same Apache server", No. node.js does not run in Apache. It runs by itself.
But, you can use multiple technologies on the same server. For starters, you could have two completely separate web servers running on separate ports, one that was Apache + PHP + webSocket and another that was node.js + webSocket.
But, node.js does not use Apache. Instead, node.js would typically be configured to be it's own web server all by itself. So, you don't typically run an Apache server and have some requests through it go to PHP and some go to node.js.
Using PHP and node.js together depends entirely upon what you're trying to do with them. You could have your web server as a node.js server and then have some requests that node.js receives actually execute some PHP and get the results from running a PHP script. And, you could do the reverse too with a PHP script fielding the request and then manually running a node.js script for some requests.
To answer any more fully, we need to know what you're really trying to do with PHP, node.js and webSockets.
FYI, you could set up an NGINX proxy on port 80 and configure it to direct some requests to your Apache/PHP server (running on a different port) and other requests to your node.js server (running on a different port). Incoming webSocket requests would then be directed to one of the two servers.
I've written a web socket server that listens to a specific port. In order to run it I log in to EC2 instance with putty and run:
php server.php
I was wondering if this is the only and the right way to do. Normally copy my php files to the host via ftp would be enough, I don't understand why the php command needs to run the server.
Any help is appreciated.
This question is not about any particular coding problem, so is considered off-topic in terms of StackOverflow.
The way PHP works - is just a script file. Same as bash (.sh), python (.py), node.js (.js) or any other similar.
They all in fact have to be executed. In common world, Apache, nginx or any other web server will do execute those scripts for you for each request is made to web server.
As you are creating socket file, you need to create it yourself, as it creates one socket and php script will continue working as long as it will by it self. It is not executed per each request. In fact make sure it is not executed by apache so do not put in usual website directory.
Let me clarify
I played with phpwebsockets and see that it requires a websocket server to run forever/constantly to keep the states etc in the memory is there any way to run websockets when there is no way of running a server on the server side?
You can used a hosted realtime service, such as Pusher who I work for. Here's comprehensive list of realtime hosted services.
There will always have to be a server of some sort listening constantly. For example, HTTP traffic is delivered by a web server (Apache, Nginx, Lighttpd, etc.) that is running constantly.
It is possible for a web server to spawn a new PHP based process, then hand the client's connection over to the new process. However, none of the major web servers currently do this, and the paradigm for web server modules that route WebSocket traffic is to send the connection over to an existing, constantly running server.
You would have to write your own custom web server in order to keep from running a WebSocket server constantly... and then, really, what's the point, since you're still writing your own constantly running server?
I want to ask a conceptual question: how a server written in php works?
Actually I want to know when I write a simple php code to get some information from a client, how does the whole process happen?
In java I have to start server first. Server listens to the port. When any client knocks then connection creates. Is it similar to php? Before running client application do I have to run my php code or server will do that for me? I am using localhost.
see this article: http://kevin.vanzonneveld.net/techblog/article/create_daemons_in_php/
Before running client application do I
have to run my php code or server will
do that for me.
This depends on your server architecture, you can use something like inet.d which invokes your script only if there is access on that port, or you have the standalone version, where you start the php from console und it waits for connections.
Does it similar to php ?
Yes.
Before running client application do I have to run my php code or server will do that for me.I am using localhost.
If you are writing a server in PHP then your PHP code is the server.