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

Playing with RabbitMQ, PHP and node
Feb 20, 2017 @ 11:51:58

In the latest post to his site Gonzalo Ayuso shares some of the results of his "playing with RabbitMQ, PHP and node", creating a queue system that both languages could talk to easily.

I need to use RabbitMQ in one project. I’m a big fan of Gearman, but I must admit Rabbit is much more powerful. In this project I need to handle with PHP code and node, so I want to build a wrapper for those two languages. I don’t want to re-invent the wheel so I will use existing libraries (php-amqplib and amqplib for node).

Basically I need to use three things: First I need to create exchange channels to log different actions. I need to decouple those actions from the main code. I also need to create work queues to ensure those works are executed. It doesn’t matter if work is executed later but it must be executed. And finally RPC commands.

He goes through some of the basics of using RabbitMQ , showing the code for each of the languages - pushing a new value into the queue, registering workers, creating Queue builders and using an exchange and receiver to process the message. The post finishes with the last piece in his requirements: creating the functionality to handle RPC commands to get an answer back from the queue.

tagged: rabbitmq node tutorial integration nodejs queue

Link: https://gonzalo123.com/2017/02/20/playing-with-rabbitmq-php-and-node/

Creating a custom queue driver for Laravel
Feb 09, 2017 @ 19:21:04

On the DotDev.co blog there's a new post showing you how to create a custom queue driver for Laravel allowing you to define the logic and handling for background job processing with the framework.

Ever needed to use a queue service not supported by Laravel? No, me neither! However, recently I needed to tweak the config for the SQS driver in order to utilise a couple of the Amazon configuration settings. Unfortunately, these settings are not natively exposed by Laravel, so I decided to build my own driver. Here’s how it went.

The tutorial walks you through the creation of the queue class that extends the "Queue" interface already built into Laravel (and what methods it requires). It then mentions the custom connector class it'll require and the service provider to link it all together. There's also a section covering the configuration you'll need to define the queue properties and what changes you'll need to make.

tagged: tutorial custom queue driver laravel interface configuration serviceprovider

Link: https://dotdev.co/creating-a-custom-queue-driver-for-laravel-3ec6463fa881#.grumknpj8

Matt Stauffer:
Update to queue workers in Laravel 5.3
Dec 21, 2016 @ 09:47:38

Continuing his series about new functionality in Laravel v5.3 Matt Stauffer has posted this quick article covering updates to the queue worker functionality.

Queues are one of those tools in Laravel that everyone knows is there, but very few people understand deeply. It's understandable--Laravel is often the first place folks have run into queues, and to be honest, they're not simple.

Thankfully, very little has changed on a user-facing front with regard to how queues work in Laravel 5.3.

One of the main updates is that the "listen" command is now "work" and the action then runs as a daemon by default instead of requiring the command to be long-running. He talks about the difference in this shift and how something like Supervisor can now be used to manage the daemon (including some documentation specific to Laravel). He finishes the post looking at what has changed "under the hood" and the benefits the changes bring.

tagged: laravel v53 update feature queue worker daemon series part16

Link: https://mattstauffer.co/blog/update-to-queue-workers-in-laravel-5-3

How To Process Tweets in Real-Time with Laravel
Apr 29, 2016 @ 09:10:16

The Scotch.io site has a tutorial posted showing you how to combine Laravel and the Twitter Streaming API to process tweets in real time inside your application.

This tutorial will show how to use the Twitter Streaming APIs to process tweets in real-time from a Laravel application. There are a variety of use cases for this: perhaps you want to auto-respond to mentions of your company, run a contest via Twitter, or create support tickets when users complain about your product. For this tutorial, we'll build a "featured tweets" widget to display approved tweets on our app's home page.

He starts with an overview of the tools and terms you'll need to know about for the tutorial and a few notes of things to watch out for. He then describes the overall structure of the application (an app just to show the tweets) and links to a repository for the impatient. He then breaks up the rest of the tutorial into several steps:

  • Creating a new Laravel application and installing the Phirehose library
  • Building a "process tweet" job and matching TwitterStream class to use Phirehose and dispatch the job
  • Make the artisan command to connect to the API and the application you created
  • Configure your queue driver and run the processing command
  • Set up a "Tweet" model to connect the application and database table

He finishes the post showing how to make the ProcessTweet Job useful, set up some basic authentication and pass the currently processed tweets into the default "Welcome" view.

tagged: realtime tweet twitter process stream api laravel job queue tutorial

Link: https://scotch.io/tutorials/how-to-process-tweets-in-real-time-with-laravel#step-11-make-the-processtweet-job-useful

Why Laravel Queues Are Awesome
Mar 18, 2016 @ 10:08:42

In a new tutorial to the Scotch.io site they take a look at Laravel's queuing functionality and explain why they think it's awesome for handling asynchronous operations in your applications.

Otwell took his time to build Laravel and it's documentation and one of the major features that really amaze me is what we are going to experiment with, Queues.

Queues in Laravel are used to make a smooth sailing application cycle by stacking heavy tasks to be handled as jobs and dispatching these jobs when it is asked to or when it does not disrupt the user's experience.

They then show how to use the queuing system to send out emails asynchronously rather than waiting for them to send via the application and making the user wait. First they show how to send them a bit more manually, making use of the Mailtrap service for testing the emails being sent. They set up a basic controller with a "send" endpoint that uses the Mail functionality to send a simple HTML email. Next they show how to refactor this into something that uses queues to handle the email sending via a SendWelcomeEmail job. He talks about performance differences between the queued and non-queued version and links to a demo repository if you'd like to see it all working together.

tagged: laravel queue tutorial introduction email mailtrap

Link: https://scotch.io/tutorials/why-laravel-queues-are-awesome

SitePoint PHP Blog:
Drupal 8 Queue API – Powerful Manual and Cron Queueing
Dec 14, 2015 @ 11:54:38

On the SitePoint PHP blog Daniel Sipos has written up a tutorial spotlighting a powerful feature of Drupal 8, the latest major release of this popular project: the Queue API. Queueing in Drupal allows you to offload tasks to be handled outside of the current web request.

The Queue API in Drupal allows us to handle a number of tasks at a later stage. What this means is that we can place items into a queue which will run some time in the future and process each individual item at that point and at least once. Usually, this happens on CRON runs, and Drupal 8 allows for a quick set up for cronjob based queues. It doesn’t necessarily have to be CRON, however.

They use two examples to help illustrate how to use the queueing system: one that uses the cron-based approach and another that's more manually triggered. They start out with the theory behind it all, talking about the different pieces (objects/classes) and how they fit together to make the queueing system. With that out of the way the article starts in on the code and the "Node Publish" queue, defining its basic structure and hooking it in to the framework. It shows you how to create the cron worker to process the queue and how to build the manual worker to do the same but only when specifically called.

tagged: drupal8 queue manual cron process defer processing tutorial

Link: http://www.sitepoint.com/drupal-8-queue-api-powerful-manual-and-cron-queueing/

SitePoint PHP Blog:
IronMQ and Laravel: Setup
May 22, 2014 @ 09:48:57

The SitePoint PHP blog has started off a series of posts showing you how to integrate the IronMQ queueing system with a Laravel framework-based application. In this first post of the series, Rajiv Seelam introduces some of the basic concepts and helps get things set up to work with the queue.

This two-part article series aims to make a beginner understand using push queues with Laravel. To give a brief overview, the final solution which we will be looking at is a simple form to upload photos and resize them. Resizing images is a time consuming task, so instead of making a user wait until the image is resized we can do it in the background. At the same time, we’ll learn to use a tool called ngrok so that we can use queues in our local system.

Their example application and the Iron.io service for handling the queuing. They walk you through the setup of the Laravel project and the installation of the other necessary libraries (including ngrok for tunneling to your localhost). Finally, they help you set up the Iron.io connection with the necessary keys and how to get Laravel to subscribe to the default queue.

tagged: laravel framework setup ironmq queue tutorial ironio

Link: http://www.sitepoint.com/ironmq-laravel-setup

SitePoint PHP Blog:
Message Queues: Comparing Beanstalkd, IronMQ and Amazon SQS
Jan 08, 2014 @ 10:37:35

The SitePoint PHP blog has a new post looking at using message queues in PHP. More specifically it compares a few of the different solutions out there and their advantages/disadvantages - Beanstalkd, IronMQ and the Amazon SQS.

This article introduces the concept of message queues and discusses the strengths and weaknesses of three specific message queue services: Beanstalkd, IronMQ and Amazon SQS. [...] Queues allow you to store metadata for processing jobs at a later date. They can aid in the development of SOA (service-oriented architecture) by providing the flexibility to defer tasks to separate processes. When applied correctly, queues can dramatically increase the user experience of a web site by reducing load times.

He starts with some of the overall benefits and downfalls of using a queueing system in your application, including some common use cases. From their he breaks it up into sections, in each talking about the option and how it differs from the others:

  • Services
  • Server setup
  • Service Level Agreements (SLAs)
  • Architecture
  • Client libraries
  • Management interface
  • Redundancy
  • Security
  • Speed
  • Fidelity
  • One-time pickup

...and many, many more. If you're looking for a good, complete overview of how these three options compare on a wide range of features and configurations, definitely check out this post. It even includes some PHP close to the end to make the connections to each and send/receive messages.

tagged: message queue compare beanstalkd ironmq amazonsqs advantage disadvantage tutorial

Link: http://www.sitepoint.com/message-queues-comparing-beanstalkd-ironmq-amazon-sqs/

Securing Login Forms From Brute-Force Attacks Using Queues
Aug 22, 2013 @ 10:22:48

On the Dreamincode.com forum there's an interesting approach mentioned for security login forms from brute-force attacks using a queueing system rather than the usual real-time requests.

Login forms in online systems are often easy targets for brute-force attacks; attacks designed to go through all possible values (or at least all probable values) for a password to "guess" a correct one. Securing your forms from such attacks is important, but it can be tricky to do in an effective manner without adversely affecting the user experience of your normal user. The method I am suggesting in this article is that of queuing login attempts in an effort to limit how many attempts an attacker can execute per second. [...] So, how do we implement this in PHP?

He suggests using a MySQL-based queueing system (but it could easily be replaced with one of the *MQ types) that tracks users versus login attempts. He's put together a library that handles the storage and login attempt management. He takes some time to explain the functionality of the class and how to put it to use.

tagged: queue login protection tutorials bruteforce

Link: http://www.dreamincode.net/forums/topic/326807-securing-login-forms-from-brute-force-attacks-using-queues/

Data Structures for PHP Devs: Heaps
Jul 23, 2013 @ 11:10:17

PHPMaster.com has posted the third part of their "Data Structures for PHP Devs" series today, this time focusing on heaps. Heaps are a method for organizing a parent/child relationship that makes it easier to work with.

In past couple articles I’ve introduced you to three basic data structures: stack, queue, and tree. In this article I’ll introduce you to another abstract data type that is closely related: heap. Heaps are specialized tree-like data structures which satisfy the heap property – the node value (key) of any parent is always ordered with respect to its child node values across the entire tree.

He starts off by explaining what the different types of heaps are - maxheap, minheap and (a special instance) a Priority Queue. He talks about the operations available to heaps and starts off with a binary maxheap implementation using arrays. He also mentions some of the functionality that the SPL already provides for this sort of thing - SplMaxHeap, SplMinHeap and the SplPriorityQueue.

tagged: data structure heap tutorial series spl priority queue

Link: http://phpmaster.com/data-structures-3