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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  5. Why?
    Different projects have
    different needs

    View full-size slide

  6. Why?
    Sockets
    Machine Learning

    View full-size slide

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

    View full-size slide

  8. Why?
    Team Efficiencies
    Employee Retention
    Continuous Learning

    View full-size slide

  9. When?
    New Projects
    New Modules

    View full-size slide

  10. When?
    Internal Projects
    Dev Tools

    View full-size slide

  11. Which?
    Feature isn’t Well
    Supported

    View full-size slide

  12. Which?
    Experts Prefer a
    Different Language

    View full-size slide

  13. Which?
    Libraries are Low Quality

    View full-size slide

  14. Implementation
    +

    View full-size slide

  15. Implementation
    Message Queues
    RPC
    APIs

    View full-size slide

  16. Implementation
    Databases
    Redis

    View full-size slide

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

    View full-size slide

  18. Implementation
    Direct Execution

    View full-size slide

  19. Implementation

    View full-size slide

  20. Implementation
    Cross Compiling
    Transpiling

    View full-size slide

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

    View full-size slide

  22. Architecture
    Capacity
    Capability

    View full-size slide

  23. Architecture
    Scaling
    Expertise

    View full-size slide

  24. Architecture
    Time to Market
    Redundancy

    View full-size slide

  25. Architecture
    Micro-Services
    OPs

    View full-size slide

  26. Business / Personal
    Engineering Growth
    Employee Retention

    View full-size slide

  27. Business / Personal
    Continuous Learning
    Skillz

    View full-size slide

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

    View full-size slide

  29. Message Queues

    View full-size slide

  30. Message Queues
    RabbitMQ
    Beanstalkd
    Redis
    Amazon SQS

    View full-size slide

  31. Message Queues
    Send and receive
    messages

    View full-size slide

  32. 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 full-size slide

  33. RPC / APIs
    RESTful APIs

    View full-size slide

  34. Message Queue
    +
    RPC

    View full-size slide

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

    View full-size slide

  36. MQ + JSON-RPC

    View full-size slide

  37. Databases / Redis

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  40. EXAMPLE TIME!

    View full-size slide

  41. MQ Example
    Message Queue

    View full-size slide

  42. MQ Example
    yo app

    View full-size slide

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

    View full-size slide

  44. 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  48. MQ Example

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

    View full-size slide

  49. 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 full-size slide

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

    View full-size slide

  51. 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 full-size slide

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

    View full-size slide

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

    });
    Create and edit processNotifications.js

    View full-size slide

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

    });
    Edit processNotifications.js

    View full-size slide

  55. 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 full-size slide

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

    View full-size slide

  57. 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 full-size slide

  58. Direct Exec Example
    Direct Execution

    View full-size slide

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

    View full-size slide

  60. Direct Exec Example

    Make sure this field is required and says "yo"





    Create and edit index.html

    View full-size slide

  61. Direct Exec Example

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  64. 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 full-size slide

  65. Direct Exec Example

    View full-size slide

  66. Direct Exec Example

    View full-size slide

  67. Direct Exec Example

    View full-size slide

  68. Direct Exec Example

    View full-size slide

  69. 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 full-size slide

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

    View full-size slide

  71. 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 full-size slide

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

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

    View full-size slide

  73. Example Source
    github.com/benedmunds/
    MoreThanJustAHammer

    View full-size slide

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

    View full-size slide

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

    View full-size slide