Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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.

Ben Edmunds

April 22, 2017
Tweet

More Decks by Ben Edmunds

Other Decks in Technology

Transcript

  1. more than
    just a
    HAMMER

    View Slide

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

    View Slide

  3. Ben Edmunds
    Open Source
    Author
    PHP Town Hall Podcast
    CTO at Mindfulware
    Who is this guy?

    View Slide

  4. View Slide

  5. Maslow’s Hammer
    If all you have is a hammer,
    everything looks like a nail
    (paraphrased)

    View Slide

  6. View Slide

  7. Why?
    Different projects have
    different needs

    View Slide

  8. Why?
    Sockets
    Machine Learning

    View Slide

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

    View Slide

  10. Why?
    Team Efficiencies
    Employee Retention
    Continuous Learning

    View Slide

  11. View Slide

  12. When?
    New Projects
    New Modules

    View Slide

  13. When?
    Internal Projects
    Dev Tools

    View Slide

  14. View Slide

  15. Which?
    Feature isn’t Well
    Supported

    View Slide

  16. Which?
    Experts Prefer a
    Different Language

    View Slide

  17. Which?
    Libraries are Low Quality

    View Slide

  18. View Slide

  19. Implementation
    +

    View Slide

  20. Implementation
    Message Queues
    RPC
    APIs

    View Slide

  21. Implementation
    Databases
    Redis

    View Slide

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

    View Slide

  23. Implementation
    Direct Execution

    View Slide

  24. Implementation

    View Slide

  25. Implementation
    Cross Compiling
    Transpiling

    View Slide

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

    View Slide

  27. View Slide

  28. Architecture
    Capacity
    Capability

    View Slide

  29. Architecture
    Scaling
    Expertise

    View Slide

  30. Architecture
    Time to Market
    Redundancy

    View Slide

  31. Architecture
    Micro-Services
    OPs

    View Slide

  32. View Slide

  33. Business / Personal
    Engineering Growth
    Employee Retention

    View Slide

  34. Business / Personal
    Continuous Learning
    Skillz

    View Slide

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

    View Slide

  36. View Slide

  37. Message Queues

    View Slide

  38. Message Queues
    RabbitMQ
    Beanstalkd
    Redis
    Amazon SQS

    View Slide

  39. Message Queues
    Send and receive
    messages

    View Slide

  40. RPC / APIs

    View Slide

  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}

    View Slide

  42. RPC / APIs
    RESTful APIs

    View Slide

  43. Message Queue
    +
    RPC

    View Slide

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

    View Slide

  45. MQ + JSON-RPC

    View Slide

  46. Databases / Redis

    View Slide

  47. Databases / Redis
    Column | Type
    ----------------------------+---------------------
    id | integer
    accepted_datetime | timestamp without time zone
    processed_datetime | timestamp without time zone

    View Slide

  48. Databases / Redis
    > SUBSCRIBE notifications
    > PUBLISH notifications yo

    View Slide

  49. EXAMPLE TIME!

    View Slide

  50. View Slide

  51. MQ Example
    Message Queue

    View Slide

  52. MQ Example
    yo app

    View Slide

  53. MQ Example

    View Slide

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

    View Slide

  55. View Slide

  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

    View Slide

  57. MQ Example
    Setup repo
    $ mkdir queueProject && cd queueProject

    View Slide

  58. MQ Example
    {
    "require": {
    "php-amqplib/php-amqplib": ">=2.6.1"
    }
    }
    $ composer install
    Create and edit composer.json
    Install packages

    View Slide

  59. MQ Example
    require_once(__DIR__ . '/vendor/autoload.php');
    use PhpAmqpLib\Connection\AMQPStreamConnection;
    use PhpAmqpLib\Message\AMQPMessage;
    Create and edit sendNotification.php

    View Slide

  60. MQ Example

    $connection = new AMQPStreamConnection(
    'localhost', 5672, 'guest', 'guest');
    $channel = $connection->channel();
    $channel->queue_declare(
    'notifications', false, true, false, false
    );
    Edit sendNotification.php

    View Slide

  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

    View Slide

  62. MQ Example
    $ php sendNotification.php
    Run our PHP script

    View Slide

  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"}
    +---------------+----------+---------------+-------------------------------------------------

    View Slide

  64. MQ Example
    {
    "dependencies": {
    "amqplib": "0.5.1"
    }
    }
    $ npm install
    Create and edit package.json
    Install packages

    View Slide

  65. MQ Example
    var amqp = require('amqplib/callback_api');
    amqp.connect('amqp://localhost', function(err, conn) {

    });
    Create and edit processNotifications.js

    View Slide

  66. MQ Example
    conn.createChannel(function(err, ch) {
    var queueName = 'notifications';
    ch.assertQueue(queueName, {durable:true});

    });
    Edit processNotifications.js

    View Slide

  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

    View Slide

  68. MQ Example
    $ node processNotifications.js
    Run the node process
    [*] Waiting for notifications

    View Slide

  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"}

    View Slide

  70. View Slide

  71. Direct Exec Example
    Direct Execution

    View Slide

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

    View Slide

  73. Direct Exec Example

    Make sure this field is required and says "yo"





    Create and edit index.html

    View Slide

  74. Direct Exec Example

    View Slide

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

    View Slide

  76. Direct Exec Example
    exports.isValidYo = function(input) {
    if (input === 'yo') {
    return true;
    }
    return false;
    };
    Create and edit modules/validator.js

    View Slide

  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

    View Slide

  78. Direct Exec Example

    View Slide

  79. Direct Exec Example

    View Slide

  80. Direct Exec Example

    View Slide

  81. Direct Exec Example

    View Slide

  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

    View Slide

  83. Direct Exec Example
    $ node nodeValidate.js "yo"
    good
    Run nodeValidate.js via Node
    $ node nodeValidate.js "fale"
    bad

    View Slide

  84. Direct Exec Example
    $input = escapeshellarg($argv[1]);
    exec('node ./nodeValidate.js ' . $input, $output, $result);
    if ($result === 0) {
    echo "";
    }
    else {
    echo "";
    }
    Create and edit phpValidate.php

    View Slide

  85. Direct Exec Example
    $ php phpValidate.php "yo"

    Run phpValidate.php
    $ php phpValidate.php "fale"

    View Slide

  86. View Slide

  87. Example Source
    github.com/benedmunds/
    MoreThanJustAHammer

    View Slide

  88. Q/A

    View Slide

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

    View Slide

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

    View Slide