[Tony Yip] libSSE-php Version 2

On 7 June, the libSSE-php released version 2.1.0 and providing well interface available to integrate with popular framework. And I release the laravel-sse for the Laravel. The libSSE-php has changed so much comparing from version 1. In this post, I hope to share about the libSSE-php version 2.

Change in libSSE-php

Namespace and packagist

First, the most important change is adding namespaces and available on packagist. To make it simple and easy to remember, I picked the prefix SSE and the packagist package name as tonyhhyip/sse. Most of the classes had the prefix SSE and not being autoload. Therefore, the prefix was changed into namespace SSE. Also, the method name is using the snake case which is not the common practice in PSR-2, as a result, all of them is then changed into lower camelcase.

Data Mechanism and Core Heart

Moreover, In the most common issue of the libSSE-php is people complain about was they are not able to change session data. To solve this issue, Data Mechanism is added for people, which is using apc, mysql and file in version 1. In version 2.0, memcache, redis and PDO mechanism is added to provide more choice for users. Furthermore, in version 2.1,  I added a Session Mechanism by making use of Symfony session interface. I make a change on the core heart – class SSE in the version 2.1, it makes use of the Symfony StreamedResponse. As a result, it is more easy to integrate the libSSE-php into the popular PHP framework like Symfony 2 and Laravel.

Release of laravel-sse

Last but not least, I release the laravel-sse, so that the library can be easily used in the most popular PHP framework nowadays.

Find out more

Hope you will love and make use the libSSE-php. Come and give it a star on Github and Packagist.
Creative Commons Licence
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Stream videos to HTML5 video container using HTTP & PHP

Sometimes we need to feed videos dynamically from the server-side. If you’re feeding the video to a HTML5 <video> element, you may find that the video progress controls freezes and users cannot move it in any ways. (Thought this situation only happens in some browsers like Chrome and Firefox, the user experience hurts a lot.)

With a bit of investigation, I found out that Chrome requested the video with an HTTP range request which, the server-side handle it incorrectly and Chrome falls back to progressive downloading the video. With the little PHP script I wrote below, the server-side can now handle the HTTP range requests normally and the progress controls no longer freezes! (There’s one more benefit: fast forward and backward works much smoother in large video files.)

Create real-time updates using Server-sent Events and libSSE

When I first learn node.js, I learned how to use socket.io, a node.js module for real-time communication between the client and server. However, when I’m going back coding PHP for a while, I started to figure out only few PHP libraries offered this functions and the techniques they uses are old (AJAX Streaming, Forever <iframe>, etc.). One day I found a new technique called Server-sent Events and it’s pretty easy to implant. I have an idea of creating an interface for Server-sent Events in order to speed up development and make it easier to use. libSSE is the library I’ve create just for this and it’s event-based which means data is sent only when an event happens. It also has some utility classes for communicate with other PHP scripts.

Here’s the library: https://github.com/licson0729/libSSE-php

It’s pretty easy to use. Here’s an example.

On the client-side you need to set up an EventSource object to listen event from server.

Remember that Server-sent Events is still a new standard and not all browsers supports it. However, polyfills are available for them. Hope you like it and find it useful.

Cross-script communication in PHP!

Sometimes we need a way to share data between scripts. However, doing so needs some setups like using MySQL as a temp. data storage, or setting up a file for storing data or use something like Memcached, APC or Redis to help. But it’s complicated when these solutions are used.

In order to solve these problems, I created a PHP class that can integrate these methods and reduce development pain. (Also, it’s originally a part of libSSE, a library for Server-sent Events.)

The code above has no dependences to libSSE itself so I decided to released it as a separate module.

I hope this can help you.

Encrypt files in PHP

Have you ever think of encrypting your secret files yourself? If you dunno how to do it yourself, here’s the rescue! In this article, I’ll teach you how to encrypt/decrypt files entirely in PHP!

First, please make sure Mcrypt is with your PHP bundle. If you don’t have this extension, please install it first as we need this to encrypt/decrypt files.

Then, it’s time to code! The code is pretty short as it uses the Mcrypt stream filter which also brings a huge performance increase when handling large files.

This is the code that encrypt contents written into the files. Isn’t this pretty short? But, how can I make the contents readable again? Here’s the way to decrypt the files:

Now we have a nice and easy solution to keep things secret in PHP. (Just to make sure, you need to change $passphrase into your very own password and keep it secret.)