Being a WordPress developer means that our main programming language is PHP. Which works for building websites but not for running tasks. In this talk I will share my experience using Node.js as a platform to build on. Explaining why I have chosen for Node.js and show you how I used Node.js to build microservices that are supporting my WordPress projects.
2. Marko Heijnen
• Founder of CodeKitchen
• Lead developer of GlotPress
• Core contributor for
WordPress
• Plugin developer
• Organizer for WordCamp
Belgrade 2015
• Technologist
9. WP Central
• Showing download history
• Showing version usage history
• List all locales and their current state (need update)
• Showing contributors data (currently API only)
• Collects history of locale progress
• Getting checksums for plugins & themes
10. How it works
• A lot of data handling by wp_remote_get
• Scrapping profiles.WordPress.org to read data
• Multiple API calls to api.WordPress.org
• Combine data so it can be presented
11. The problem
• Most things happened through WP Cron
• Some things happens on the front end
• Resulting a load on the webserver that could and
should be prevented
14. Service server
• MariaDB as database
• Memcached as object cache
• Moving to Redis when the PHP7 version is out
• Elasticsearch to make search better/faster
15. The microservices server
• Handles all cronjobs for the network site
• Node.js services running for WP Central
• Like getting checksums for plugins/themes
• Soon merging other WP cronjob calls for getting
all the stats
18. Benefits
• Different services can use different programming
languages
• High level separation
• If WordPress breaks, the services still keep running
• Ease of Deployment
• Scale services that require more resources
19. Benefits
• In general they have an (REST) API
• Reusable
• Other microservices could call the service to run a
task
21. What is Node.js
• JavaScript platform
• Uses an event-driven, non-blocking I/O model
• Lightweight and efficient
• Ideal for real time application
• Lot’s of modules you can use
• Manage with NPM - https://www.npmjs.org
22. Why to use it
• Internal webserver
• No configuration needed outside it’s code base
• You could use nginx as a proxy but not needed
• You get what you see approach
24. Need to know modules
• Express / Restify -> Webserver
• Socket.io -> Real time
• Request -> Doing internet requests
• async -> async calls with callback
• mysql -> MySQL driver with Pool support
• node-cmd -> Command line
26. What is does
• Request checksum of a certain version from a
plugin or theme
• Download the zip and unzips it.
• Reads it in memory and get the checksum per
entry
• After everything is retrieved stores it in MySQL
27. Modules used
• Build in modules
FS
Crypto
• NPM Modules
Express
MySQL
Request
Yauzl for unzipping
• And build a little queue class
28. Calling the API
• http://wpcentral.io/api/checksums/plugin/tabify-
edit-screen/0.8.3 (REST API)
• Calls nginx by IP (10.10.10.10) which handles as a
fallback when the node.js application is down
• nginx calls then internally the node.js application
like proxy_pass http://127.0.0.1:8080
32. Basic setup
// set variables for environment
var express = require('express'),
app = express(),
mysql = require('mysql'),
request = require('request'),
fs = require('fs'),
crypto = require('crypto'),
yauzl = require("yauzl");
33. MySQL connection
var pool = mysql.createPool({
connectionLimit : 10,
host : ’10.10.10.11’,
user : 'checksums',
password : 'checksums',
database : 'checksums'
});
pool.on('enqueue', function () {
log_error('Waiting for available connection slot');
});
37. Starting the server
• The default way is: node server.js
• The production server way could be:
pm2 start server.js -u www-data --name “Cool service”
39. The new situation
• No more unneeded logic in WordPress
• WordPress simple pipes the calls
• Small services that replacing it
• APIs can easily be reused
• Pushing new updates becomes easier
• Currently no caching but easily added
42. Other ideas
• Scheduling tasks or url
calls
• Build a central cache
point for external
sources like getting
tweets
• Real time support
• git2svn sync
• Backup service
• Real time logger
• Perform heavy tasks