Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io from php source

I have developed a client-server connection by using socket.io, and i'm happy to say that it works perfectly. The problem comes because I need to create that connection from php Source ( not "echo html javascript" ). Something Like a

$socket = socket.createConectionJS;

I've tried difrerent ways, such as :

  • execute the code from the server ( with spiderMonkey, and node )
  • creating a phpSocket and connecting it to the ServerSocket.j( but obbiously the format is diferent). or ...
  • Finally i've tried by post with curl, where I had access to the serverscript, but i can't open a connection

I'm thinking the only way to resolve it is simulating the web socket connection by creating a socket with the same way as de socket.io class do it but in php

Anny socket.io expert could help ?

Tnks ! ! _

like image 718
Marlon Avatar asked Jun 03 '11 15:06

Marlon


2 Answers

Yeah, I had a quick go at it just then. I'm fortunate to have a fairly advanced web scraping library already built in PHP, so I just plugged the XML from TamperData (firefox plugin), into it, and tweaked a few things.

To emulate xhr-polling (my proxy doesn't allow websockets, and this looked simpler anyway)...

Make a request to:

/socket.io/1/?t=1337779479761   

(The 13377... number is just a timestamp, use time() to generate it).

That should return something like this:

682970926640665221:60:60:websocket,htmlfile,xhr-polling,jsonp-polling

Grab the big number at the front out, that's your "[CONNECT_ID]", which you'll keep for the remainder of the session. Now do another request:

/socket.io/1/xhr-polling/[CONNECT_ID]?t=[TIMESTAMP]

And you'll get back something like ::1

That's about as far as I bothered to follow it, it all looked fairly basic from there... no special headers or anything sneaky. Suggest you use TamperData or a packet sniffer, and just follow it yourself. Here was the output from my code:

$ php RealTestCurl.php xml/xhr.xml init1 xhr1 xhr1 xhr1 xhr1

xmlFilename: xml/xhr.xml

Step: init1
Reply: 7638339571841585529:60:60:websocket,htmlfile,xhr-polling,jsonp-polling
Found: connect_id: ([0-9]*) - 7638339571841585529

Step: xhr1
Reply: 1::

Step: xhr1
Reply: ?46?5:::{"name":"news","args":[{"hello":"world"}]}?63?5:::{"name":"this","args":[{"will":"be received by everyone"}]}

Step: xhr1
.... there is a massive 20 second timeout here

Step: xhr1
8::

Step: xhr1
8::

And on the node.js/socket.io side, running on of the basic examples from their front page:

debug - client authorized
info  - handshake authorized 3445861131360107212
debug - setting request GET /socket.io/1/xhr-polling/3445861131360107212?t=1337781185
debug - setting poll timeout
debug - client authorized for 
debug - clearing poll timeout
debug - xhr-polling writing 1::
debug - set close timeout for client 3445861131360107212
debug - websocket writing 5:::{"name":"this","args":[{"will":"be received by everyone"}]}
like image 170
Orwellophile Avatar answered Oct 13 '22 00:10

Orwellophile


We developed and use in production Elephant.io

We basically use it in our server cron-jobs to notify our front, and in our Symfony2 APIs to push some events to the front.

Have a look, it might help you.

Best

like image 45
guillaumepotier Avatar answered Oct 13 '22 02:10

guillaumepotier