Pro Yearly is on sale from $80 to $50! »

Lone Star PHP 2017 - More Than Just a Hammer

Lone Star PHP 2017 - More Than Just a Hammer

Learn to expand beyond just PHP to integrate other languages into both your personal skill set and the solutions you deliver.

In this talk we'll cover integrating JavaScript (frontend and backend via node) and how you can expand your current skill set to include this or any other language alongside PHP. The old saying, if all you have is a hammer everything looks like a nail, is very true so let's learn how to use a wrench and maybe a chainsaw!

Not only will this give you another tool in your tool belt, it will give you a greater understanding of development practices and the ability to on new jobs, new projects, and new challenges.

22f21d5c22b930fd35dd98f25dedf6a4?s=128

Ben Edmunds

April 22, 2017
Tweet

Transcript

  1. more than just a HAMMER

  2. Ben Edmunds @benedmunds http://benedmunds.com Who is this guy?

  3. Ben Edmunds Open Source Author PHP Town Hall Podcast CTO

    at Mindfulware Who is this guy?
  4. None
  5. Maslow’s Hammer If all you have is a hammer, everything

    looks like a nail (paraphrased)
  6. None
  7. Why? Different projects have different needs

  8. Why? Sockets Machine Learning

  9. Why? Code Re-use X-App Communication

  10. Why? Team Efficiencies Employee Retention Continuous Learning

  11. None
  12. When? New Projects New Modules

  13. When? Internal Projects Dev Tools

  14. None
  15. Which? Feature isn’t Well Supported

  16. Which? Experts Prefer a Different Language

  17. Which? Libraries are Low Quality

  18. None
  19. Implementation +

  20. Implementation Message Queues RPC APIs

  21. Implementation Databases Redis

  22. Implementation Inter-Process Comm (IPC) Shared Memory Semaphores

  23. Implementation Direct Execution

  24. Implementation

  25. Implementation Cross Compiling Transpiling

  26. Atwood’s Law Any application that can be written in Javascript,

    will eventually be written in Javascript
  27. None
  28. Architecture Capacity Capability

  29. Architecture Scaling Expertise

  30. Architecture Time to Market Redundancy

  31. Architecture Micro-Services OPs

  32. None
  33. Business / Personal Engineering Growth Employee Retention

  34. Business / Personal Continuous Learning Skillz

  35. Business / Personal (negatives) Context Switching Overhead Hiring

  36. None
  37. Message Queues

  38. Message Queues RabbitMQ Beanstalkd Redis Amazon SQS

  39. Message Queues Send and receive messages

  40. RPC / APIs

  41. RPC / APIs JSON-RPC (client) —> (server) {"jsonrpc": "2.0", "method":

    "sendNotification", "params": {"id": 123, "sender_id": 1}, "recipient_id": 2, "message": "yo"} (client) <— (server) {"jsonrpc": "2.0", "result": 1, "id": 123}
  42. RPC / APIs RESTful APIs

  43. Message Queue + RPC

  44. MQ + JSON-RPC Send callback queue in message

  45. MQ + JSON-RPC

  46. Databases / Redis

  47. Databases / Redis Column | Type ----------------------------+--------------------- id | integer

    accepted_datetime | timestamp without time zone processed_datetime | timestamp without time zone
  48. Databases / Redis > SUBSCRIBE notifications > PUBLISH notifications yo

  49. EXAMPLE TIME!

  50. None
  51. MQ Example Message Queue

  52. MQ Example yo app

  53. MQ Example

  54. MQ Example PHP-Amqplib https://packagist.org/packages/php-amqplib/php-amqplib amqplib https://www.npmjs.com/package/amqplib

  55. None
  56. MQ Example Install the RabbitMQ server on Mac OS $

    brew install rabbitmq $ echo 'export PATH=/usr/local/sbin:$PATH' >>~/.bash_profile Append to your path $ rabbitmq-server Start the server
  57. MQ Example Setup repo $ mkdir queueProject && cd queueProject

  58. MQ Example { "require": { "php-amqplib/php-amqplib": ">=2.6.1" } } $

    composer install Create and edit composer.json Install packages
  59. MQ Example <?php require_once(__DIR__ . '/vendor/autoload.php'); use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Message\AMQPMessage;

    Create and edit sendNotification.php
  60. MQ Example … $connection = new AMQPStreamConnection( 'localhost', 5672, 'guest',

    'guest'); $channel = $connection->channel(); $channel->queue_declare( 'notifications', false, true, false, false ); Edit sendNotification.php
  61. MQ Example … $notification = [ 'id' => microtime(), 'sender_id'

    => Auth::user()->id, 'recipient_id' => $recipientUser->id, 'message' => 'yo' ]; $msg = new AMQPMessage(json_encode($notification)); $channel->basic_publish($msg, '', 'notifications'); Edit sendNotification.php
  62. MQ Example $ php sendNotification.php Run our PHP script

  63. MQ Example Check our queue for data $ rabbitmqadmin get

    queue=notifications +---------------+----------+---------------+------------------------------------------------- | routing_key | exchange | message_count | payload +---------------+----------+---------------+------------------------------------------------- | notifications | | 0 | {"sender_id":1,"recipient_id":2,"message":"yo"} +---------------+----------+---------------+-------------------------------------------------
  64. MQ Example { "dependencies": { "amqplib": "0.5.1" } } $

    npm install Create and edit package.json Install packages
  65. MQ Example var amqp = require('amqplib/callback_api'); amqp.connect('amqp://localhost', function(err, conn) {

    … }); Create and edit processNotifications.js
  66. MQ Example conn.createChannel(function(err, ch) { var queueName = 'notifications'; ch.assertQueue(queueName,

    {durable:true}); … }); Edit processNotifications.js
  67. MQ Example console.log('[*] Waiting for notifications'); ch.consume(queueName, function(msg) { ch.ack(msg);

    console.log("[x] Received %s", msg.content.toString()); }); Edit processNotifications.js
  68. MQ Example $ node processNotifications.js Run the node process [*]

    Waiting for notifications
  69. MQ Example $ php sendNotification.php Run our PHP script Output

    from Node [*] Waiting for notifications [x] Received {"id":"0.36937600 1492376196", "sender_id": 1,"recipient_id":2,"message":"yo"}
  70. None
  71. Direct Exec Example Direct Execution

  72. Direct Exec Example Setup repo $ mkdir directProject && cd

    directProject Create modules dir $ mkdir modules
  73. Direct Exec Example <html><body> <h1>Make sure this field is required

    and says "yo"</h1> <input type="text" name="thing" id="thing" /> <input type="button" value="Validate!" onClick="validate()" /> <script type="text/javascript" src="modules/require.js" /> <script type="text/javascript" src="modules/index.js" /> </body></html> Create and edit index.html
  74. Direct Exec Example

  75. Direct Exec Example https://github.com/letorbi/smoothie Add CommonJS compatible loader to modules/require.js

  76. Direct Exec Example exports.isValidYo = function(input) { if (input ===

    'yo') { return true; } return false; }; Create and edit modules/validator.js
  77. Direct Exec Example function validate() { var validator = require('modules/validator');

    if (validator.isValidYo(document.getElementById('thing').value)) { alert("They're good YOs Brent"); } else { alert("BAD!"); } } Create and edit index.js
  78. Direct Exec Example

  79. Direct Exec Example

  80. Direct Exec Example

  81. Direct Exec Example

  82. Direct Exec Example var validator = require('./modules/validator'); if (validator.isValidYo(process.argv[2])) {

    console.log('good'); process.exit(0); } else { console.log('bad'); process.exit(1); } Create and edit nodeValidator.js
  83. Direct Exec Example $ node nodeValidate.js "yo" good Run nodeValidate.js

    via Node $ node nodeValidate.js "fale" bad
  84. Direct Exec Example <?php $input = escapeshellarg($argv[1]); exec('node ./nodeValidate.js '

    . $input, $output, $result); if ($result === 0) { echo ""; } else { echo ""; } Create and edit phpValidate.php
  85. Direct Exec Example $ php phpValidate.php "yo" Run phpValidate.php $

    php phpValidate.php "fale"
  86. None
  87. Example Source github.com/benedmunds/ MoreThanJustAHammer

  88. Q/A

  89. Give Me $$$ SecuringPhpApps.com Resources SecuringNodeApps.com

  90. THANKS! Ben Edmunds @benedmunds http://benedmunds.com https://joind.in/talk/6a845