Game Development with ReactJS and PHP: How Compatible Are They? — SitePoint

  • I added my recent work with PHP preprocessors to make things cleaner, and made the first couple of endpoints:
    $host = new 8080);

    $host-use($router = = = process = process is from config.pre

    I decided to use Aerys for the HTTP and Web Socket portions of the application.

  • Finally I added pre/short-closures, both to handle the custom syntax in config.pre and the short closures I planned on using after…
    Then I set about creating routes files:
    use Aerys\Router;
    use App\Action\HomeAction;

    return (Router $router) = {
    $router-route(
    “GET”, “/”, new HomeAction
    );
    };

    This is from routes/web.

  • Finally, I created two (similar) actions:
    namespace App\Action;

    use Aerys\Request;
    use Aerys\Response;

    class HomeAction
    {
    public function __invoke(Request $request,
    Response $response)
    {
    $response-end(“hello world”);
    }
    }

    This is from final touch was to add shortcut scripts, to launch dev and prod versions of the Aerys server:
    “scripts”: {
    “dev”: “vendor/bin/aerys -d -c loader.php”,
    “prod”: “vendor/bin/aerys -c loader.php”
    },
    “config”: {
    “process-timeout”: 0
    },

    This is from composer.json

    With all of this done, I could spin up a new server, and visit http://127.0.0.1:8080 just by typing:
    composer dev

    Setting Up The Front-end
    “Ok, now that I’ve got the PHP side of things relatively stable; how am I going to build the ReactJS files?

  • I noticed Mix liked to create a manifest file, so I made a helper function to query it:
    use Amp\Coroutine;

    function mix($path) {
    $generator = () = {
    $manifest = yield $manifest = json_decode($manifest, true);

    if (isset($manifest[$path])) {
    return $manifest[$path];
    }

    throw new Exception(“{$path} not found”);
    };

    return new is from helpers.pre

    Aerys knew how to handle promises when they came in the form of $val = yield $promise, so I used Amp’s Promise implementation.

  • I found this relatively straightforward, with a new class:
    namespace App\Socket;

    use Aerys\Request;
    use Aerys\Response;
    use Aerys\Websocket;
    use Aerys\Websocket\Endpoint;
    use GameSocket implements Websocket
    {
    private $endpoint;
    private $connections = [];

    public function onStart(Endpoint $endpoint)
    {
    $this-endpoint = $endpoint;
    }

    public function onHandshake(Request $request,
    Response $response)
    {
    $origin = if ($origin !

Chris bootstraps a basic Stardew-Valley-like game in this game development with PHP post, using an async server, preprocessors, and ReactJS!
Continue reading “Game Development with ReactJS and PHP: How Compatible Are They? — SitePoint”

How to Scan Fingerprints with Async PHP and React Native — SitePoint

How to multi-factor authentication with Fingerprints scans, Async PHP and React Native

  • Our composer.json file could look something like:
    {
    “scripts”: {
    “dev”: “vendor/bin/aerys -d -c loader.php”,
    “prod”: “vendor/bin/aerys -c loader.php”
    },
    “require”: {
    “amphp/aerys”: “dev-amp_v2”,
    “pre/kitchen-sink”: “^0.1.0”
    },
    “autoload”: {
    “psr-4”: {
    “App\\”: “app”
    }
    },
    “config”: {
    “process-timeout”: 0
    },
    “minimum-stability”: “dev”,
    “prefer-stable”: true
    }

    This is from composer.json in the server project

    We can also set up a placeholder for requesting scans, along with an HTTP GET route to get to it:
    namespace App\Action;

    use Aerys\Request;
    use Aerys\Response;

    class ScanAction
    {
    public function __invoke(Request $request, Response $response)
    {
    $response-end(“requesting a scan…”);
    }
    }

    This is from app/Action/ScanAction.

  • pre in the server project

    use Aerys\Router;
    use App\Action\ScanAction;

    return (Router $router) = {
    $router-route(
    “GET”, “/scan”, new ScanAction
    );
    };

    This is from routes/web.

  • pre in the server project

    And we can start the server up with a server config file and pre-processor loader script:
    $port = 8080;

    $host = new $port);

    $host-use($router = Aerys\router());

    $web = process ‘http://127.0.0.1:{$port}’ | pbcopy”);

    This is from config.pre in the server project

    return Pre\processAndRequire(__DIR__ . “

  • Let’s add web sockets to our server:
    namespace App\Socket;

    use Aerys\Request;
    use Aerys\Response;
    use Aerys\Websocket;
    use Aerys\Websocket\Endpoint;
    use FingerprintSocket implements Websocket
    {
    private $endpoint;
    private $connections = [];

    public function onStart(Endpoint $endpoint)
    {
    $this-endpoint = $endpoint;
    }

    public function onHandshake(Request $request,
    Response $response)
    {
    // $origin = $request-getHeader(“origin”);
    //
    // if ($origin !

  • == “http://127.0.0.1:8080”) {
    // $response-setStatus(403);
    // $response-end(“origin not allowed”);
    // return null;
    // }

    $info = return $info[“client_addr”];
    }

    public function onOpen(int $clientId, $address)
    {
    $this-connections[$clientId] = $address;
    }

    public function onData(int $clientId, Message $message)
    {
    $body = yield $message;

    yield $this-endpoint-send(
    $payload, $clientId
    );
    }

    public function onClose(int $clientId,
    int $code, string $reason)
    {
    }

    public function onStop()
    {
    // nothing to see here…
    }
    }

    This is from in the server project

    use Aerys\Router;
    use App\Action\ScanAction;
    use (Router $router) = {
    $router-route(
    “GET”, “/scan”, new ScanAction
    );

    $router-route(
    “GET”, “/ws”, Aerys\websocket(new FingerprintSocket)
    );
    };

    This is from routes/web.

In this comprehensive and advanced tutorial, Chris shows us how to scan fingerprints with Async PHP and React Native. The future is here!
Continue reading “How to Scan Fingerprints with Async PHP and React Native — SitePoint”

Game Development with ReactJS and PHP: How Compatible Are They? — SitePoint

Game Development with #ReactJS and PHP: How Compatible Are They?

  • I added my recent work with PHP preprocessors to make things cleaner, and made the first couple of endpoints:
    $host = new 8080);

    $host-use($router = = = process = process is from config.pre

    I decided to use Aerys for the HTTP and Web Socket portions of the application.

  • Finally I added pre/short-closures, both to handle the custom syntax in config.pre and the short closures I planned on using after…
    Then I set about creating routes files:
    use Aerys\Router;
    use App\Action\HomeAction;

    return (Router $router) = {
    $router-route(
    “GET”, “/”, new HomeAction
    );
    };

    This is from routes/web.

  • Finally, I created two (similar) actions:
    namespace App\Action;

    use Aerys\Request;
    use Aerys\Response;

    class HomeAction
    {
    public function __invoke(Request $request,
    Response $response)
    {
    $response-end(“hello world”);
    }
    }

    This is from final touch was to add shortcut scripts, to launch dev and prod versions of the Aerys server:
    “scripts”: {
    “dev”: “vendor/bin/aerys -d -c loader.php”,
    “prod”: “vendor/bin/aerys -c loader.php”
    },
    “config”: {
    “process-timeout”: 0
    },

    This is from composer.json

    With all of this done, I could spin up a new server, and visit http://127.0.0.1:8080 just by typing:
    composer dev

    Setting Up The Front-end
    “Ok, now that I’ve got the PHP side of things relatively stable; how am I going to build the ReactJS files?

  • I noticed Mix liked to create a manifest file, so I made a helper function to query it:
    use Amp\Coroutine;

    function mix($path) {
    $generator = () = {
    $manifest = yield $manifest = json_decode($manifest, true);

    if (isset($manifest[$path])) {
    return $manifest[$path];
    }

    throw new Exception(“{$path} not found”);
    };

    return new is from helpers.pre

    Aerys knew how to handle promises when they came in the form of $val = yield $promise, so I used Amp’s Promise implementation.

  • I found this relatively straightforward, with a new class:
    namespace App\Socket;

    use Aerys\Request;
    use Aerys\Response;
    use Aerys\Websocket;
    use Aerys\Websocket\Endpoint;
    use GameSocket implements Websocket
    {
    private $endpoint;
    private $connections = [];

    public function onStart(Endpoint $endpoint)
    {
    $this-endpoint = $endpoint;
    }

    public function onHandshake(Request $request,
    Response $response)
    {
    $origin = if ($origin !

Chris bootstraps a basic Stardew-Valley-like game in this game development with PHP post, using an async server, preprocessors, and ReactJS!
Continue reading “Game Development with ReactJS and PHP: How Compatible Are They? — SitePoint”

How to Scan Fingerprints with Async PHP and React Native — SitePoint

How to Scan Fingerprints with Async PHP and #reactnative  #reactjs

  • Our composer.json file could look something like:
    {
    “scripts”: {
    “dev”: “vendor/bin/aerys -d -c loader.php”,
    “prod”: “vendor/bin/aerys -c loader.php”
    },
    “require”: {
    “amphp/aerys”: “dev-amp_v2”,
    “pre/kitchen-sink”: “^0.1.0”
    },
    “autoload”: {
    “psr-4”: {
    “App\\”: “app”
    }
    },
    “config”: {
    “process-timeout”: 0
    },
    “minimum-stability”: “dev”,
    “prefer-stable”: true
    }

    This is from composer.json in the server project

    We can also set up a placeholder for requesting scans, along with an HTTP GET route to get to it:
    namespace App\Action;

    use Aerys\Request;
    use Aerys\Response;

    class ScanAction
    {
    public function __invoke(Request $request, Response $response)
    {
    $response-end(“requesting a scan…”);
    }
    }

    This is from app/Action/ScanAction.

  • pre in the server project

    use Aerys\Router;
    use App\Action\ScanAction;

    return (Router $router) = {
    $router-route(
    “GET”, “/scan”, new ScanAction
    );
    };

    This is from routes/web.

  • pre in the server project

    And we can start the server up with a server config file and pre-processor loader script:
    $port = 8080;

    $host = new $port);

    $host-use($router = Aerys\router());

    $web = process ‘http://127.0.0.1:{$port}’ | pbcopy”);

    This is from config.pre in the server project

    return Pre\processAndRequire(__DIR__ . “

  • Let’s add web sockets to our server:
    namespace App\Socket;

    use Aerys\Request;
    use Aerys\Response;
    use Aerys\Websocket;
    use Aerys\Websocket\Endpoint;
    use FingerprintSocket implements Websocket
    {
    private $endpoint;
    private $connections = [];

    public function onStart(Endpoint $endpoint)
    {
    $this-endpoint = $endpoint;
    }

    public function onHandshake(Request $request,
    Response $response)
    {
    // $origin = $request-getHeader(“origin”);
    //
    // if ($origin !

  • == “http://127.0.0.1:8080”) {
    // $response-setStatus(403);
    // $response-end(“origin not allowed”);
    // return null;
    // }

    $info = return $info[“client_addr”];
    }

    public function onOpen(int $clientId, $address)
    {
    $this-connections[$clientId] = $address;
    }

    public function onData(int $clientId, Message $message)
    {
    $body = yield $message;

    yield $this-endpoint-send(
    $payload, $clientId
    );
    }

    public function onClose(int $clientId,
    int $code, string $reason)
    {
    }

    public function onStop()
    {
    // nothing to see here…
    }
    }

    This is from in the server project

    use Aerys\Router;
    use App\Action\ScanAction;
    use (Router $router) = {
    $router-route(
    “GET”, “/scan”, new ScanAction
    );

    $router-route(
    “GET”, “/ws”, Aerys\websocket(new FingerprintSocket)
    );
    };

    This is from routes/web.

In this comprehensive and advanced tutorial, Chris shows us how to scan fingerprints with Async PHP and React Native. The future is here!
Continue reading “How to Scan Fingerprints with Async PHP and React Native — SitePoint”

Optimizing React Performance with Stateless Components — SitePoint

Optimizing React Performance with Stateless Components  #reactjs #javascript

  • First, the Super Basics
    import React, { Component } from ‘react’

    class User extends Component {
    render() {
    const { name, highlighted, userSelected } = this.props
    console.log(‘Hey User is being rendered for’, [name, highlighted])
    return div
    h3
    style={{fontStyle: highlighted ?

  • For example, something like this:
    import React, { Component } from ‘react’

    class Users extends Component {
    constructor(props) {
    super(props)
    this.state = {
    otherData: null,
    users: [{name: ‘John Doe’, highlighted: false}]
    }
    }

    async componentDidMount() {
    try {
    let response = await let data = await response.json()
    this.setState({otherData: data})
    } catch(err) {
    throw err
    }
    }

    toggleUserHighlight(user) {
    this.setState(prevState = {
    users: prevState.users.map(u = {
    if (u.name === user.name) {
    u.highlighted = !

  • import React, { PureComponent } from ‘react’

    class User extends PureComponent {

    render() {
    const { name, highlighted, userSelected } = this.props
    console.log(‘Hey User is being rendered for’, [name, highlighted])
    return div
    h3
    style={{fontStyle: highlighted ?

  • Our first attempt at re-writing it back to a functional component but with recompose.pure looks like this:
    import React from ‘react’
    import { pure } from ‘recompose’

    const User = pure(({ name, highlighted, userSelected }) = {
    console.log(‘Hey User is being rendered for’, [name, highlighted])
    return div
    h3
    style={{fontStyle: highlighted ?

  • ‘italic’ : ‘normal’}}
    onClick={event = {
    userSelected()
    }}{name}/h3
    /div
    })

    export default User

    As you might notice, if you run this, the User component still re-renders even though the props (the name and highlighted keys) don’t change.

Writing inefficient React components can cause them to rerender too often. Peter Bengtsson looks at ways of creating and optimizing stateless components.
Continue reading “Optimizing React Performance with Stateless Components — SitePoint”

Game Development with ReactJS and PHP: How Compatible Are They? — SitePoint

Game development with #ReactJS and #PHP: How compatible are they?  #JavaScript

  • I added my recent work with PHP preprocessors to make things cleaner, and made the first couple of endpoints:
    $host = new 8080);

    $host-use($router = = = process = process is from config.pre

    I decided to use Aerys for the HTTP and Web Socket portions of the application.

  • Finally I added pre/short-closures, both to handle the custom syntax in config.pre and the short closures I planned on using after…
    Then I set about creating routes files:
    use Aerys\Router;
    use App\Action\HomeAction;

    return (Router $router) = {
    $router-route(
    “GET”, “/”, new HomeAction
    );
    };

    This is from routes/web.

  • Finally, I created two (similar) actions:
    namespace App\Action;

    use Aerys\Request;
    use Aerys\Response;

    class HomeAction
    {
    public function __invoke(Request $request,
    Response $response)
    {
    $response-end(“hello world”);
    }
    }

    This is from final touch was to add shortcut scripts, to launch dev and prod versions of the Aerys server:
    “scripts”: {
    “dev”: “vendor/bin/aerys -d -c loader.php”,
    “prod”: “vendor/bin/aerys -c loader.php”
    },
    “config”: {
    “process-timeout”: 0
    },

    This is from composer.json

    With all of this done, I could spin up a new server, and visit http://127.0.0.1:8080 just by typing:
    composer dev

    Setting Up The Front-end
    “Ok, now that I’ve got the PHP side of things relatively stable; how am I going to build the ReactJS files?

  • I noticed Mix liked to create a manifest file, so I made a helper function to query it:
    use Amp\Coroutine;

    function mix($path) {
    $generator = () = {
    $manifest = yield $manifest = json_decode($manifest, true);

    if (isset($manifest[$path])) {
    return $manifest[$path];
    }

    throw new Exception(“{$path} not found”);
    };

    return new is from helpers.pre

    Aerys knew how to handle promises when they came in the form of $val = yield $promise, so I used Amp’s Promise implementation.

  • I found this relatively straightforward, with a new class:
    namespace App\Socket;

    use Aerys\Request;
    use Aerys\Response;
    use Aerys\Websocket;
    use Aerys\Websocket\Endpoint;
    use GameSocket implements Websocket
    {
    private $endpoint;
    private $connections = [];

    public function onStart(Endpoint $endpoint)
    {
    $this-endpoint = $endpoint;
    }

    public function onHandshake(Request $request,
    Response $response)
    {
    $origin = if ($origin !

Chris bootstraps a basic Stardew-Valley-like game in this game development with PHP post, using an async server, preprocessors, and ReactJS!
Continue reading “Game Development with ReactJS and PHP: How Compatible Are They? — SitePoint”

Redux without React — State Management in Vanilla JavaScript

Redux without #ReactJS — State Management in Vanilla #JavaScript:

  • Other modules might then listen to changes in the store and update themselves .
  • Pure presentational component filenames are prefixed with a dollar sign.
  • The component subscribes to changes in the store
  • Container components on the other hand can dispatch actions or subscribe for changes.
  • The components had access to the store via my failed connect approach.

Moritz Kröger discusses his experiences of using Redux without React — the problems  faced, the solutions attempted and the lessons learned along the way.
Continue reading “Redux without React — State Management in Vanilla JavaScript”