Socket programming with phpPresentation Transcript
Back to the Basics
In the beginning
There was a process
and the process ran well
but it needed to talk to another process
“Use IPC and all shall be clear”
Inter Process Communication
But which do I need?
Are the processes on the same computer?
Does it need to support multiple OS types?
Communication needs to be one way or two way?
All things internet
Layers of Fun
IP – forwards packets of data based on a destination
TCP – verifies the correct delivery of data from client to
server with error and lost data correction
Network Sockets – subroutines that provide TCP/IP (and
UDP and some other support) on most systems
The DOD section DARPA built ARPANET which ran
on TCP/IP and the protocols are maintained by IETF
TCP - Transmission Control Protocol
IP – Internet Protocol
UDP - User Datagram Protocol
Packet of Data
What are Sockets?
Sockets are just like "worm holes" in science fiction.
When things go into one end, they (should) come out of
Doing it with PHP
“Oh yeah, I guess PHP does have all those apis…”
Making things dead easy
What is a Stream?
Access input and output generically
Can write and read linearly
May or may not be seekable
Comes in chunks of data
How PHP Streams Work
Bidirectional network stream that speaks a protocol
Tells a network stream how to communicate
Tells a stream how to handle specific protocols and
Things to watch for!
feof means “connection_closed”?
huge reads or writes (think 8K)
stream_get_meta_data is READ ONLY
All the power of C apis, all the pain of C apis
Almost C, but not quite
Wrapper around BSD sockets api
Some of the declarations are different
You really need to know how sockets work
Like writing in C without the hassles
A core extension but NOT a default extension
Possibly noisy, use an error handler
Keep your man handy
C man page
domain (IPv4, IPv6, unix)
type – stream for tcp and dgram for udp is the usual
protocol – hint, you can pass 0 to get the “default”
protocol for the type
gives you a NEW socket resource to read off of, but you
can’t accept off the newly created socket resource it
address (ipv4 or 6 depending on what you set)
Read and Write
len (will truncate to this!)
type (stop on newlines or grab as binary)
socket_recv - usually connected socket
socket_recvfrom - from a specific address and port
socket_recvmsg – grabs a whole message (datagram’s
socket_shutdown – stop reading and writing
socket_close – all done, close it down
what – no poll?
Poll vs. Select
select - has existed for a great while and exists almost
poll - Not existing on older unixes nor in Windows
before Vista. Broken implementation in Mac OS X at
least up to 10.4
There’s a lot more!
READ THE C MAN PAGES!
TCP on bidirectional full-duplex acid
New and shiny
Take the place of ajax, polling, and other hackery
Allow cross origin communications
based on TCP
look like an “upgrade” request to HTTP clients/servers
reduce latency and overhead
full-duplex on a single socket
usable with proxies
allow polling and streaming without pain
persistent…. wait - persistent?
Hmmm - persistent
Websockets are persistent connections
Your $server_of_choice may not like this
newer nginx is smart about proxying them
apache has some websockets modules
IIS8 and higher has support as well
use a standalone php server
If using fastcgi/php-fpm, you’re going to eat processes
How DO they DO it?
NIH will kill you
Rachet - http://socketo.me
Wrench - https://github.com/varspool/Wrench
PHPWS - https://github.com/Devristo/phpws