chat with socket ajax php and nodejs? - php

I am writing a chat application for mobile & web using PHP and nodejs-socket.io.
I am using Android to post messenger to PHP - PHP to create a new socket (net library on nodejs) and that response messenger to friend via nodejs.
A (via HTTP Request)-> PHP (via socket)-> NODEJS (via socket.io)-> B
(web - websocket / android)
I was trying A->NODEJS->B use all by socket.io, but the message is slow when the number of users increases.
I found the time for sending messenger from nodejs to B device (chatter) very fast, but socket PHP sending messenger to NODEJS is slow.
I see the socket in PHP/nodejs is opening and closing many times in a short period of time.
Please help me make my application run faster, or suggest a solution?
I expected to try using java, but can anyone tell me anything about websocket performance, i.e. socket.io vs. java socket speeds?
Thank you.

Websocket performance really good, really fast. I tested on Chrome (PC) but not on mobile phone yet.
And Java socket is simple and easy to use. Why don't you use java socket to connect directly from Android -> NODEJS without making any HTTP request and PHP bridge?
The reason why PHP socket repeatedly open and close: There are many reason make your code open and close repeatedly, may be there are no loop in your PHP code to keep the connection. Can you show your detailed code of the problem please?

Related

Websockets with PHP and Symfony - network and server architecture

We have a web application that currently uses polling to handle continuous updates.
While switching to long polling may be a small bandaid to the issue, we wanted to implement a Websockets solution that will be durable and scalable.
My question is: what kind of architecture will be needed for this?
I have done some research myself and have found that the typical setup for an enterprise application is something like this:
The execution flow of a connection then would be something like this:
Initial handshake
Client makes an HTTP Request to Server along with JS to request a Websocket connection
Server responds, Header contains the Upgrade directive and switches protocols for that client
Websocket Server establishes a Websocket connection with client
Client submits a POST/PUT/etc.
Webserver (Apache/Nginx) fetches result from query
Webserver sends result to the MQ where it is added to the queue
Message is sent to Websocket server from MQ
Message is sent back to the client
Is this approach correct? Am I missing anything? Am I understanding something incorrectly?
Try having a look at this: socketo.me
Kinda gives you an idea. From what I can see, this is fairly similar to what you've envisioned in the architecture diagram you posted.
May also be worth looking at this Stackoverflow question's answer.
Have you tried GOS WebSocketBundle ?
I'have used it on production site to handle chat and along with stunnel it works flawlessly over SSL.

PHP script running as Socket Server and Client simultaneously

For a new project I need to implement remote desktop protocols. The addresses of the remote need to be secured and may never get sent to the client. After a lot of research and some tests I found Guacamole, which also has a Java client. The project is designed as an API though, so I started porting some bits of the Java client example to PHP.
The use case will be the following:
User logs into my service (Laravel application)
WebSocket connection establishes to a constantly running PHP script (using HOA\WebSocket)
Upon authorization a TCP socket needs to be established to the Guacamole Daemon
Commands coming via WebSocket need to be directed to the Guacamole Daemon and vice versa
What makes this complicated is the fact that the application needs to be able to serve multiple clients simultaneously. Multiple TCP sockets need to be established and multiple WebSocket connections need to be managed all at once.
For my simple test I opened the socket via fsockopen and then looped to wait for data. With this I obviously can't listen to multiple sockets at once (at least realistically), but I stumbled upon the React Socket Client library:
Think of this library as an async version of fsockopen() or stream_socket_client().
This sounds like it is what I need, but then again, I'm using HOA and its WeSocket server, which apparently also runs in a loop (when invoking WebsocketConnectionHandler->run()).
Should I even be using React's Socket Client or should I try to use HOA's Socket library instead (seeing as I'm already using WebSocket from that)? Are React and HOA even compatible in their event loop, so could I listen to WebSocket clients and a TCP connection at the same time?
If so, could anyone give me some hints or examples on how to get started with coupling these two? Thanks!

Website to connect to a client desktop application

I have an application written in VB.net that runs on a clients pc.
I also have a website written in mostly javascript, http and php.
The thing I want to do is to connect the website to the application, so that when i.e. a certain button is pressed, it connects to the client application and raises an event.
I have tried approaches like TCP socket communication by having a TCP Socket Server running in the background of the client application. I can connect to the server by having a client connection from another vb.net application, but whenever I try to connect through PHP it fails. (I have only tried PHP since server-side scripting seems to make more sense in this case)
Another approach I have tried is to have an HTTP server running in the background of my desktop application and then have a PHP script connect to it, that fails as well.
One thing that I've been thinking about as a last resort is to simply have a textfile on the webserver and a PHP script writing to it after given parameters and then have the client application to read the file every few seconds. But this wouldn't be very efficient with larger amounts of data, would it?
What is the proper way of doing this?
If you have any questions about the code I've been using, feel free to ask.
If you don't get my blurry explanation, try this image: http://i.imgur.com/8njxVFj.png
Thanks in advance.
To have your data more organized i would suggest you to store your data on a database server for example mysql (which is free).

PHP Socket Help Understanding

Im trying to understand how a socket works in PHP.
Lets say I have a file called socket.php, and this creates a socket bound to my localhost on port 99.
Then I run the socket in a while loop so it's constantly connected.
is there a function in PHP to make calls to that socket while its listening?
Another question is: If I have another service such as Java running on a socket -- is it a bad idea to use PHP to connect to the socket to make a call. I ask because I could potentially be recreating new socket connections many, many times.
So is having to reconnect to a socket hundreds of times in PHP bad? Or should I re-use the same socket connection somehow? (I am thinking in terms of AJAX calls to PHP which connects to a Java Socket).
Edit: You can see the example code: https://github.com/JREAM/sandbox/tree/master/php
Im trying to communicate with in socket.php and socket_send.php -- I am leaving socket.php running and opening another console and running socket_send.php and trying to get a result into the console.
Answer to your first Question: I suggest going over here everything you need about sockets is there. Basically the function you want to use is socket_read or socket_recvfrom if using UDP.
Answer to your second Question: Sockets are just a way to send messages to services. It doesn't matter if a client is in php and the server is in Java. Think of it this way. Does it matter that you are viewing a web-page on a linux Web Server with a windows Box?

Website to computer communications

I am working on my senior project at university and I have a question. My advisor and other workers don't know much more on the matter so I thought I would toss it out to SO and see if you could help.
We want to make a website that will be hosted on a server that we are configuring. That website will have buttons on it, and when visitors of that website click a certain button we want to register an event on the server. We plan on doing this with PHP.
Once that event is registered (this is where we get lost), we want to communicate with a serial device on a remote computer. We are confident we can set up the PHP event/listener for the button press, but once we have that registered, how do we signal to the remote computer(connected via T1 line/routers) to communicate with the serial device? What is this sequence of events referred to as? The hardest thing for us (when researching it) is that we are not certain what to search for!
We have a feeling that a python script could be running on the server, get signals from the PHP listener, and then communicate with the remote PC. The remote PC could also be running a python script that then will communicate with our serial device. Again, most of this makes sense, but we are not clear on how we communicate between Python and PHP on the web server (or if this is possible).
If any one could give me some advice on what to search for, or similar projects I would really appreciate it. Thanks,
Both php and python can communicate via sockets, so I guess that is a good bet.
In PHP, when you receive the signal from the click button, open a socket to your python app with socket_create.
In python, you would need to implement a server socket (SocketServer) that listens for a connection.
Try reading up on sockets communication in general and socket programming in php and python specifically.
You can set up a web server also on the remote computer, perhaps using the same software as on the public server, so you do not need to learn another technology. The public server can make HTTP requests and the remote server responds by communicating with the serial device.

Categories