WebSocket with PHP
WebSocket with PHP
Get your coffee!
Topics▷ What is WebSocket;▷ Browser Support;▷ About Ratchet;▷ Difficulties;▷ Examples;▷ Conclusion;▷ Questions.
Hello!I'm Leonardo Rifeli▷ Back-end developer at ContaAzul▷ I'm 20 years old▷ I developed more than 100
websites▷ Writer at leonardorifeli.com▷ And I'm a Java developer too
contaazul.com/carreiras
Work with us!
What is WebSocket?Definition of WebSocket
Definition of WebSocket
Basicamente, é uma conexão bi-direcional de um navegador web para um servidor. Depois que uma conexão socket é estabelecida a conexão permanece aberta até que o cliente ou servidor decida encerrar.
O cliente ou servidor pode enviar uma mensagem a qualquer outro cliente conectado. Uma única aplicação de servidor em execução estará ciente de todas as conexões abertas.
WebSocket connection example
HTTP connection example
NO, IS TECHNOLOGY!
Where to use?Ok! But, where use it?
ChatsYes! They are developed with socket communication.
Some Implementations
GamesThe games that use real-time update. Message by users.
Some AppsExample (Google Docs):
Browser SupportThey already support connection with
socket?
More info: https://goo.gl/0D8cf9
Yes! Except the Opera!
Perfect!
WebSocket ServerAbout Ratchet PHP WebSockets
About RatchetRatchet is a loosely coupled PHP library providing developers with tools to create real time, bi-directional applications between clients and servers over WebSockets. This is not your Grandfather's Internet. (socketo.me description).
Ratchet is fully PSR-0 compliant, so it naturally plays well with others. Leveraging components from Symfony2, development should feel similar to many.
WebSocket connection flow
Basic ImplementationHow to implement the Ratchet?
Implementation
▷ Ratchet\Server\IoServer: Creates an open-ended socket to listen on a specific port. Events are delegated through this to attached applications.
▷ Ratchet\Http\HttpServer: Implement the interface MessageComponentInterface and manages the connections.
▷ Ratchet\WebSocket\WsServer: The adapter to handle WebSocket requests/responses. his is a mediator between the Server and your application to handle real-time messaging through a web browser.
▷ Hermes\Business\Service\SocketService: It will be the implementation of websocket methods and contain all business rule.
Difficulties▷ Secutiry;▷ Latency;▷ Many connections;▷ And other points ranging from
business rule.
Simple Example!
References
▷ WebSocket no PHP: https://goo.gl/Zfysuy▷ Ratchet - PHP WebSockets:
https://goo.gl/UGX00E▷ Apresentando WebSockets: trazendo soquetes
para a web: https://goo.gl/KEZfWZ▷ More code examples: https://goo.gl/ChpAsd
Thanks!Any [email protected]@leonardorifeli