Jump to content

PHP and Websockets


NotionCommotion

Recommended Posts

I have two server/clients.  Server B initiates communication to Server A.  Afterwards, both servers need to be able to initiate communication to one another.  The router in front of Server B should not require any special configuration such as port forwarding.

 

  1. Is this possible using PHP?  I recognize maybe PHP is maybe not the preferred language, but wish to use it as proof of concept.
  2. If possible, please provide very high level steps on how it can be accomplished.

 

Thank you

 

LOutYhL.png

Link to comment
Share on other sites

This is possible with PHP, yes. You don't need to use WebSockets though unless you plan on having a web browser client. You could just use a regular TCP/UDP socket instead.

 

The firewall would need to allow communication on the selected port.

 

Start with the manual.

 

Thank you Scootstah,

 

No, there is no web browser client, and I will check sockets out.  Something I've always planned on doing, but never really did in earnest.  Note that assuming the firewall is configured to allow communication on standard ports such as 80 or 443 is acceptable, but setting up port forwarding to a given local IP is not.  It sounds like I am okay in this regard, but if you feel differently, please advise.

 

Thanks again!

 

EDIT.  Will some network configurations prevent this?  Somehow Server A needs to get back to Server B, and without port forwarding or equal, the local IP needs to be somehow bundled in the response.

Edited by NotionCommotion
Link to comment
Share on other sites

Your questions are so open its hard to tell you anything without a million clauses...

The router will need either port forwarding if its your own, else server B needs to be in the DMZ (basically same thing if I remember right). Is the router your home router or an actual internet gateway?

 

The bit no-one has mentioned is that server B has a dynamically assigned IP, is that on the internal network, or is that the router itself or does that have a static IP? If so, then server B needs to start the communication to server A's static IP, or it needs to indicate somewhere what its IP is for server A to contact it.


If this is all using web servers, then if a dynamically assigned IP client server (i.e. previously unknown) contacts the host server, then the following may be used to get it's details (note that some of these may be empty, spoofed or dangerous):

$remote_addr=@$_SERVER['REMOTE_ADDR'];
$remote_host=@$_SERVER['REMOTE_HOST'];
if($remote_host==""){$remote_host = gethostbyaddr($remote_addr);}
$refer=@$_SERVER['HTTP_REFERER'];
Link to comment
Share on other sites

 

The router will need either port forwarding if its your own, else server B needs to be in the DMZ (basically same thing if I remember right). Is the router your home router or an actual internet gateway?

 

The bit no-one has mentioned is that server B has a dynamically assigned IP, is that on the internal network, or is that the router itself or does that have a static IP? If so, then server B needs to start the communication to server A's static IP, or it needs to indicate somewhere what its IP is for server A to contact it.

 

Don't want port forwarding or a DMZ.

 

For arguments sake, lets say Server B is located behind my home router, and my router has a semi-static public IP as defined by my IP provider.

 

If I am using a browser behind my home router, it is my understanding that I could initiate a websocket to a server, and later receive pushes from that server.

 

If a browser could do it, why could it not be done without one?

Edited by NotionCommotion
Link to comment
Share on other sites

Don't want port forwarding or a DMZ.

 

For arguments sake, lets say Server B is located behind my home router, and my router has a semi-static public IP as defined by my IP provider.

 

If I am using a browser behind my home router, it is my understanding that I could initiate a websocket to a server, and later receive pushes from that server.

 

If a browser could do it, why could it not be done without one?

 

In this case then server B would have to initiate the connections.

Link to comment
Share on other sites

In this case then server B would have to initiate the connections.

 

Yes, I totally agree.  I tried to make that clear in the original post.

 

Assuming that server B initiated the connection, will http://php.net/manual/en/book.sockets.php work?  My initial review indicated that they are not sophisticated enough to pass header information to return to originating client.  If not, how can this be accomplished?

Link to comment
Share on other sites

Yes, I totally agree.  I tried to make that clear in the original post.

 

Assuming that server B initiated the connection, will http://php.net/manual/en/book.sockets.php work?  My initial review indicated that they are not sophisticated enough to pass header information to return to originating client.  If not, how can this be accomplished?

 

All networks are based on sockets, e.g. Berkley Sockets, WinSock, etc...

 

IMHO sockets are too low level for you (but then again I have no idea at what you're doing). Personally when web programming I've never had to use / manage them, I only go that low when using C or C++, etc... My main point is, if you're communicating with a web server then all the sockets are already managed for you and you just use functions like file_get_contents() or maybe cURL, etc.

 

Apache and NGINX, setup pools of threads that manage sockets, they then pass the data to the PHP parser (sort of), which is technically the application (even though the server can be thought of as the application when considering the OSI layers.)

 

 

 

 

Soooo... what are you wanting to do, then someone can suggest the ideal solution, e.g. maybe things like SOAP or REST for different types of API demands, etc...

  • Like 1
Link to comment
Share on other sites

Thanks 0X00,

 

Sorry for being cryptic, but I am not allowed to communicate the application.

 

What I heard you say is "yes, this is possible, but not really with PHP and will require some lower level language such as C, etc."  If I misread you, please advise.

 

PS.  I like your comment "All networks are based on sockets, e.g. Berkley Sockets, WinSock, etc..."  It confirms what I somewhat already suspected.

 

Thanks again, Michael

Link to comment
Share on other sites

Thanks 0X00,

 

Sorry for being cryptic, but I am not allowed to communicate the application.

 

What I heard you say is "yes, this is possible, but not really with PHP and will require some lower level language such as C, etc."  If I misread you, please advise.

 

PS.  I like your comment "All networks are based on sockets, e.g. Berkley Sockets, WinSock, etc..."  It confirms what I somewhat already suspected.

 

Thanks again, Michael

 

I don't use raw sockets much anymore because there are so many libraries which handle them for you.

 

Setting up sockets is very fiddly and thats before handling all the possible signals, threads and security concerns. That's why we now use web servers for many things, it works out the box, but it may not be right for your circumstance, but there is probably a library, framework or program which will be.

 

I'm not asking for your million pound idea, but what it does and its constraints would help, well not me but you.

Link to comment
Share on other sites

You can't use ports 80 or 443 if you have a webserver installed. You can't bind to the same port.

 

If you can't open ports then you can't use sockets.

 

 

No, they use whatever port the daemon is listening on.

 

It's not possible to use sockets without an open port to listen on.

 

Agree a port needs to be open to hear through that port.

 

Doesn't websockets by default use 80 and 443?  I don't claim to know, and base this on https://en.wikipedia.org/wiki/WebSocket.

 

If so, could a server that responds to websockets not act as a webserver?

 

Needless to say, I am very new to sockets!

Link to comment
Share on other sites

Do you need a persistent connection?

 

Most web servers weren't designed to handle persistent connections, they may even have a limited thread pool to work with (i.e. a max number of concurrent connections).

 

Most realtime games use persistent tcp connections, and many games baulk out at 20 or so concurrent connections, yes there are ways to handle more, but, well... another story!

Link to comment
Share on other sites

Doesn't websockets by default use 80 and 443?  I don't claim to know, and base this on https://en.wikipedia.org/wiki/WebSocket.

No, websockets do not default to anything. The protocol does not mention implementation details such as port numbers.

 

If so, could a server that responds to websockets not act as a webserver?

Sure, you could bind your socket daemon to listen on port 80 or 443. But then you can't run a webserver like Apache.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.