Jump to content

PHP socket server - how to make socket client in browser

- - - - -

  • Please log in to reply
2 replies to this topic

#1
__ak

__ak

    Newbie

  • Members
  • PipPip
  • 24 posts
Hi all,

I've been creating a TCP socket server in PHP.
I started by reading this tutorial: Writing Socket Servers in PHP and have edited the final script quite a bit.
It's working really well when telnet'ing from terminal and so on.

But my question is how to make a live client side to work in browser?
I've previously made a http server with node.js where I used socket.io on the frontend (and on the backend with node.js) - but it doesn't seem like it's possible to link socket.io with PHP sockets?
- I think I might have misunderstood something (maybe the http server part) - after all, I'm quite new to socket programming.

I've looked into using a hidden flash file for the socket connection - but I'd really like if there's another (maybe Javascripty) way around this?

Any help is appreciated!

- __ak

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
  • Location:New York, NY
Writing server daemons in PHP is (excuse the bluntness) a dumb idea. PHP is not the language for that. The current trend seems to be shifting toward node.js for ad-hoc server implementations.

Is there any reason you are making a TCP server? Normally, the underlying HTTP server that you are using (Apache/Lighthttpd/IIS) can be used for everything (In my 7 years of PHP programming, I've never had the need to use something different). Using an AJAX request to some PHP script that outputs JSON works quite well for asynchronous activities.

That being said, the HTML 5 specification includes WebSockets which allow you to open direct connections to non-HTTP socket servers. This will probably be used for streaming media in the future to avoid the overhead of HTTP headers, it also allows the server to notify the browser immediately when something changes rather than having to have the browser poll the server every few milliseconds. The major drawback to this is that relatively few browsers support the WebSockets API in its entirety. So at this point, its not a good idea to rely on it.

#3
__ak

__ak

    Newbie

  • Members
  • PipPip
  • 24 posts
Hi John,
Thanks for your reply!

I'm aware that PHP isn't the ideal tool for the job - but I'm simply doing it to explore the field of socket programming.
PHP is my "native" language, so I did find it a good place to start.

I've made som Ajax based chats before with a PHP+MySQL backend - but never used sockets before.
But what is the idea of using sockets if I'll have to make a new connection every time I need to recieve data?

I'm hoping to find a 100% live solution like I can get with node.js+socket.io

The Websockets in HTML5 - are they "implementable" with PHP sockets - or do I need some kind of tricky JS libary?




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users