Node.js to the rescue

1,049 views

Published on

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.

Published in: Engineering
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,049
On SlideShare
0
From Embeds
0
Number of Embeds
26
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Node.js to the rescue

  1. 1. Marko Heijnen CODEKITCHEN Node.js to the rescue
 Let Node.js do things when WordPress/PHP isn’t enough
  2. 2. Marko Heijnen • Founder of CodeKitchen • Lead developer of GlotPress • Core contributor for WordPress • Plugin developer • Organizer for WordCamp Belgrade 2015 • Technologist
  3. 3. Why something else?
  4. 4. Yes, you can build almost everything in WordPress
  5. 5. But is that the smart thing to do?
  6. 6. Can you trust on WordPress to be always stable?
  7. 7. This summer I started moving things away from WordPress
  8. 8. WP Central
  9. 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. 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. 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
  12. 12. New server setup
  13. 13. Loadbalancer Memcached Elasticsearch MariaDB New server setup Micro services Webserver 1 Webserver 2 Thumbor Public Private
  14. 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. 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
  16. 16. Microservices
  17. 17. Microservices • Microservices are small, autonomous services that work together • Small, and Focused on Doing One Thing Well
  18. 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. 19. Benefits • In general they have an (REST) API • Reusable • Other microservices could call the service to run a task
  20. 20. Node.js
  21. 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. 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
  23. 23. Who is using it • Netflix
  24. 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
  25. 25. Checksums for plugins/themes
  26. 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. 27. Modules used • Build in modules
 FS
 Crypto • NPM Modules
 Express
 MySQL
 Request
 Yauzl for unzipping • And build a little queue class
  28. 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
  29. 29. API calls • /plugin/:slug/:version
 http://10.10.10.10/checksums/plugin/:slug/:version 
 
 http://wpcentral.io/api/checksums/plugin/tabify- edit-screen/0.8.3 • /theme/:slug/:version
 http://10.10.10.10/checksums/theme/:slug/:version
 
 http://wpcentral.io/api/checksums/theme/ twentyfourteen/1.2
  30. 30. nginx rules error_page 404 @404;
 error_page 500 @500;
 error_page 502 @502; location @404 {
 internal;
 add_header Content-Type application/json always;
 return 404 '{ "status": "Route Not Found" }';
 } return 500 '{ "status": "Service is down" }';
 return 502 '{ "status": "Service is down" }';
  31. 31. Going over the code
  32. 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. 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'); });
  34. 34. Server app.listen(4000); app.get( '/plugin/:slug/:version', function(req, res) { if ( ! queue.add( 'plugin', req.params.slug, req.params.version, res ) ) { res.json({ 'success': false, 'error': 'Generating checksums’ }); } });
  35. 35. Server 404 app.use(function(req, res, next) { res.status(404).json({ 'success': false, 'error': "Route doesn;'t exist” }); });
  36. 36. Lets check the rest
  37. 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”
  38. 38. The new situation
  39. 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
  40. 40. Other things you could use node.js for
  41. 41. See my presentation:
 Extending WordPress as a pro Describing an idea of using Socket.io with WordPress
  42. 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
  43. 43. Thank you for listening Questions? @markoheijnen markoheijnen.com
 
 codekitchen.eu

×