Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

SitePont PHP Blog:
How to Quickly Build a Chat App with Ratchet
Sep 28, 2015 @ 15:52:41

On the SitePoint PHP blog they've posted a tutorial showing you how to build a chat application with Ratchet, a library in PHP that lets you work with WebSockets for more real-time communication between your frontend and backend systems.

In this tutorial, we’ll be taking a look at Ratchet, a PHP library for working with WebSockets. Let’s start by defining what WebSockets are. MDN says: "WebSockets is an advanced technology that makes it possible to open an interactive communication session between the user’s browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply. Connected computers image."

WebSockets allow us to write applications that can pass data from the browser to the server and vice-versa in real-time.

They start by helping you get Ratchet installed (via Composer) and start immediate in on the code. They create the application namespace, a Chat class and client to handle the WebSocket connection. They create methods for the open, close, write and read for the connection and make a simple script with a Ratchet server to handle the messaging back and forth. They also explain the various pieces of the system: the IoServer, HttpServer and WsServer. Finally they help you create the HTML frontend (using jQuery and Handlebars) and the CSS/Javascript to finish off the functionality.

tagged: ratchet websocket realtime chat application tutorial library

Link: http://www.sitepoint.com/how-to-quickly-build-a-chat-app-with-ratchet/

Gonzalo Ayuso:
Building TCP server daemond with PHP and Rachet
Apr 13, 2015 @ 15:18:41

Gonzalo Ayuso has a new post to his site today showing how to create a TCP server daemon with PHP with help from the Ratchet toolset. Ratchet is a library that makes it easier to work with WebSockets directly from PHP.

In my daily work I normally play a lot with TCP servers, clients and things like that. I like to use Linux’s xinet.d daemon to handle the TCP ports. I’ve also written something about it. This approach works fine. The problem appears when we call intensively our xinet.d server. It creates one PHP instance per request. It isn’t a problem with one request in, for example, 3 seconds, but if we need to handle 10 requests per second our server load will grow. The solution: a dedicated server.

In a setup similar to how Silex registers callbacks, he's created a PHP-based server that listens on whatever ports are defined for incoming connections and processes the data accordingly. He includes several code samples that show it in use, both in simple request handling and more complex configurations based off of a YAML file definition. He ends the post with a method he uses to "emulate" threading in his processing with the help of a Silex app and HTTP requests to hand off the processed and remove the blocking problem PHP introduces.

tagged: tcp server daemon ratchet websocket silex tutorial

Link: http://gonzalo123.com/2015/04/13/building-tcp-server-daemon-with-php-and-rachet/

Thomas Weinert:
Building A Sensor Phalanx With PHP
Nov 18, 2013 @ 15:15:12

Thomas Weinert continues his series looking at merging the physical and virtual using PHP to with messages coming from servers and how to build it into a sensor phalanx.

Sensors are fun. They report from the physical world into the digital. But getting the signal into php is only the first part, you will have to get them out again. This post shows how to get data from analog sensors pushed to the browser. It uses Carica Chip, if you haven't read my previous blog post you should do it first.

He talks about the use of ReactPHP in the Carica library and the addition of Ratchet to help with the websocket messaging. He sets up two simple servers - one HTTP, the other websocket - to handle the reporting and interaction with the sensor. He includes the code for the HTTP server and uses his example code to make the phalanx listening on 8081. Some other code is included to make the listeners and the simple UI for the charts. A video is included showing it all in action, reacting to a moving light.

tagged: sensor phalanx tutorial carica chip library reactphp ratchet

Link: http://www.a-basketful-of-papayas.net/2013/11/building-sensor-phalanx-with-php.html

SitePoint PHP Blog:
Building a Live-score Widget Using PHP Web Sockets
Oct 17, 2013 @ 15:11:42

On the SitePoint PHP blog there's a new post from Lukas White about using PHP and web sockets to create a "live score" widget to include in your site.

The introduction of web sockets makes it possible for web applications to handle near real-time data without resorting to "hacks" such as long-polling. One example of an application requiring up-to-the-minute data is sports scores. Even now, many websites which display this information use Flash applications, since Actionscript provides the facility to communicate over socket-based connections. However, web sockets allow us to replicate this functionality using only HTML and Javascript. That's what we're going to build in this tutorial, along with a lightweight "server" in PHP.

His example uses the Ratchet PHP library to provide the WebSockets functionality to the frontend script polling for the latest data. He helps you get the library installed and set up a simple directory structure for the example. He includes sample scripts for both the data provider (for the scores) and the WebSocket provider. He talks about maintaining the state of the data and shows how to pull out random data from a set of fixtures (pre-defined data) to send back to the frontend. The Javascript for the frontend script is also included.

tagged: tutorial websockets live score example javascript ratchet

Link: http://www.sitepoint.com/building-live-score-widget-using-php-web-sockets/

Pivory.com:
From Ajax to WebSocket with PHP, a Quick Example
Aug 31, 2012 @ 15:41:28

On Pivory.com there's a new topic (tutorial) showing you a simple example of using WebSockets with the Ratchet PHP library, complete with sample code.

WebSocket is the modern way for realtime two-way communications between server and browser. In the beginning we have Ajax calls [...] and with WS in picture the logic becomes [different]. [There] is a separate WS server, say listening on xxx.xxx.xxx.xxx:8080, and it can send messages to connected clients in realtime at any moment. The WS server can be in any language (such as node.js + socket.io) and on any machine or even in a cloud service. I use PHP (try http://socketo.me for Ratchet) because I am lazy.

The example shows a basic PHP Web Socket server running on a port with methods for "on connect", "on close", "on error" and "on message send". There's an example of the Javascript code to connect to this socket, showing how to convert it over from Ajax usage.

tagged: websockets tutorial ratchet library server

Link:


Trending Topics: