 Hi. Can you hear me? Okay. Yeah. So, I'm going to talk about my journey, implementing async and await in PHP, which is not a standard feature, which is available now. So, let me talk about myself. I'm the author of Resla framework, which is an open source framework that can host. You can write a PHP class, define some public and protected methods. Once you hook that up with the framework, it will become a web API, a RESTful API that you can consume. So, for example, you can see a class, which is using another class for the address as a parameter. With these two definitions, we will be able to create an API and also a web UI so you can get a form like this with validation and all. Everything automatically done through these two classes. So, that is one. And I'm the head of technologies at Logical Steps, where I work with blockchain-related technologies. And Ola Search is a platform, which is a search, intelligent search as a service. And for you guys, our free offerings include WordPress plugin and Laravel Scout engine for Ola Search, as well as search API client for PHP. Do try them out. Okay. So, PHP can do async. Many of us are not really using it yet. Right? How many of you are using asynchronous PHP? Okay. So, I only see a few hands. So, how we can do asynchronous PHP? There are a few ways. There are some extensions that you can use. There are some libraries you can use. One simple way of implementing could be just using curl as an async system. So, if you make curl calls on a non-blocking fashion, you can do, you can process multiple curl requests at once, one without blocking the other. And there are several server libraries. If I have more time, I will show more of them. So, one of which is React PHP, which is already mentioned in today's talks. And Facebook's hack is also capable of asynchronous callback. So, the idea of a callback, which is very familiar to us, right? If you are going to take quite a bit of time to respond back to me, why do I need to hold the line? I will simply ask you to call me back. When you are finished, just call me back so that I can focus on something else. My time is well spent, right, instead of just holding the line. But then that can be done in two ways, right? It can be done in a synchronous fashion, like this guy who is, like, passionately waiting for the call. I don't know what call he is waiting for. He is not going to do anything. He is not going to move from that place until he gets the call. So, that is synchronous execution. So, we make a call to the server, for example, and wait for the response. Until the response comes back, we are not doing anything, right? On the other hand, if you are doing the same thing in a non-blocking fashion, we will be able to make the request and do something else while the response comes back and then continue handling that response. So, typically, in PHP world, we do the synchronous, do the things in the synchronous fashion, right? Even the call call, until the response comes, it will hold there, then only it will execute the next line. And in the JavaScript world, we are used to asynchronous handling. So, there are, when you want to do something else instead of just waiting, whenever you are doing an operation which doesn't involve your work, basically like reading from the disk or writing to a disk or reading from the network or communicating with the DB, you ask the DB until the DB comes back to you and serves you back, you have nothing to do. You can focus on something else and be more productive. And then sending email, all those things take time, right? When it takes time and you have nothing to do in that particular process, your time can be spent better elsewhere. So, when we talk about call back, this situation happens. So, that staircase should be familiar for you guys, if you have written call back, your code will look like that staircase, right? So, keeping one call back inside another so that you can execute in a sequence, so it will become a big staircase. That is what we call the call back hell, right? We guys are familiar with that in the JavaScript world itself. And we have to deal with that in the PHP world as well. So, this is my motivation. So, I had to deal with the blockchain. I have to talk to an Ethereum blockchain and get some account information and then, like, sum up all the balances of individual accounts so that I know how much of total is there in the system, okay? If I have to do that, because every call is basically, is a JSON RPC call to the blockchain, which gives me the response later, I need to write the code like this. This is something I hated. I'm used to C-shop doing Async and Await. And now we have Async and Await in JavaScript as well. So, I wanted to find a way to do the same in PHP. So, the same code which I've shown you just now, it can be written this way if it is synchronous. If it just returns the response immediately, this is what I will do. Okay? Can the code be this simple and still do the same thing that the Asynchronous code is doing? That is the question that I had. I worked on that. In a previous PHP user group meetup, I showed this. I showed a demo of how it can be done. And I promised that I will release the source code, open source, today. I will do that right after the event. Thank you. So, let me show a bit about how Async and Await work in JavaScript. So, you put the keyword Async in front of a function, and then you put Await wherever it is supposed to wait for the response to come back. So, this will happen in a non-blocking fashion unless, like, if you are using sleep in PHP, which is a blocking operation, it won't go anywhere. It will just simply wait there, right? Instead, here, it will just continue executing other processes. For this function, it will wait there. That's all. So, this is how Async and Await works in JavaScript. So, taking this as an inspiration, I came up with this. So, we have something in PHP for quite some time called generator, which is a very interesting feature. It can be used for many things. How many of you are already using generators? Okay, only a few. You guys should check out generators. That is, like, really cool. What generator gives you is a possible function. You write a function and put an yield statement in it. It becomes a generator. That means wherever there is an yield statement, you can pass the function there. If you are executing PHP in a way that it continues to execute, usually, the life span of PHP is too short, right? We use InginX or Apache. That is what handles the web request, usually. And then pass it, pass that to a PHP file, usually the index.php. And that loads the framework, loads everything and then understands the route that you have passed in. So, it processes the request, generates the response, spits the response, and dead. All the thing it did, it clears out, it is gone. But if you can execute it asynchronously with multiple processes and all, you can replace the InginX and Apache with PHP itself, in which there can be some performance improvement that can be found. Because the entire loading of the framework application that can stay in the memory, only the request and response that is supposed to change between every request and response, right? So, that can be handled individually. Everything else can stay in memory. That way it will be like very performant. That is what frameworks like React, PHP, and all offer. I will talk more about that later. So, how does this work? Now that I know if there is an yield statement, I can pass there, any function that needs to be called. So, let me just go back to this callback code. As you can see with that Ethereum library, you can call and pass any parameter. The last parameter you pass will be a callback, which will take two parameters. First one will be the error and second will be the actual response of the function. So, that is the mechanism that typical callbacks use. So, I can emulate that. And in order to not execute the function beforehand, what I can do is I can keep it as a callable. So, what is callable in PHP? If you give a string that denotes a function which can be executed, that is a callable. And then what else? If you give an array of an object or a class and the method name, that is a callable. So, I take that concept and extend it further. So, as you can see in the first statement, I mean, the third line, accounts is equal to yield an array of ETH and accounts. So, that is a callable. I take that and I create an engine that can run this generator, go through each and every possible step and execute it adding a callback. And only when the result comes back, I will continue the execution. That is how the whole thing works. So, this is something I demoed in the PHP meetup. And I can show that the result, the supposed result comes back, which you can see in the commented code. Okay. So, as I said, any function, you add an yield statement there, it becomes a generator. So, the return value of that function will be a generator. So, any object of a generator class has these methods. So, usually you can check whether a generator is valid, whether all the yields are done or not. If still there is a yield that it is, if it is still passing, it hasn't finished, it will be valid. Once it is finished with all the yields, it won't be valid. So, then I can get the return type and all if it is invalid. If it is valid, I can send some value into the generator and also I can read the value from there and execute the flow. Okay. So, I will show a demo of a very simple function. If I take Ethereum code and show it, it will be too complex, right? So, I support these scenarios basically. Like, I have a function called two seconds. It will use React PHP's timer to delay for two seconds, not sleep. So, after two seconds, it should resume. And then I have a class called timer, which has a delay function, which can take a parameter. So, I'm passing eight as a parameter for that class. That means delay for eight seconds. Okay. So, this example is completely just delay execution. Just wait, wait, wait. Just to to the point that it works. But practically you can use it for many things. You can wait for many things to occur. So, one flow can wait for another flow. And this can be like very powerful. Say, for example, if you are just serving a request and dying, then you cannot do much. But if you are executing a PHP process that can live for days, months and years, it can do a lot. This is a workflow engine in a way, right? So, think about an approval process. So, the manager has to sign. Then only the next person can execute it. All those things can be written in a single function. It looks like a function. It acts like a function. But it is happening asynchronously. And you don't have to do anything. You just execute through the engine, which will take care of that. Okay. So, the next one basically creates an instance. So, previous one is a static function. Timer delay is a static function. The next one is a instance of timer. And there is a function called wait, which does the same thing. So, this is just to showcase different ways of executing asynchronous code. And then timer hold is a function that is a generator, basically. And then it has a yield statement. So, it will wait until that. So, when it is a generator, which is automatically, it can be passed, right? By default it is passed. Unless you run it, it is not going to run. So, you can directly execute, call the function there. But if you are calling a generic function, which has a callback and all, you cannot call beforehand. You have to call it through the flow, right? That's why we are passing them as an array. And I'm extending the syntax of callable, which is basically the object and the method name. I'm adding the parameters also into this. So, that the execution engine can look for the parameter and apply the parameter, add the callback as one more parameter. And when that callback is called, then it will continue the execution. And similarly, React PHP has a promise class. So, you can return a promise. Promise is basically saying that I will fulfill this later. When I'm done, I will tell you, you can call a method called then and pass a function. That will be called when the job is done. So, it can handle a promise also and wait for the promise to be fulfilled to continue the execution. Also, it can process the return value. So, here timer.hold is a generator function that will also be awaited. So, let me show the actual code. So, this is a two seconds function. So, is it readable? Are you guys able to see it? Okay. So, I just create a loop, which is a React PHP's loop. And then, yeah, there is a function, two seconds, which takes a callback. So, using the global loop object for the brevity, it just adds a timer. When that timer happens, it just calls back. So, this is one. And then delay, which is a static function, which I've talked about before. Where it is a non-static function. So, it has to be called with an instance. And as I told you, the hold is basically yielding another function. Basically, it is internally calling wait for the delay. And then, the last one is a promise. Okay. So, this is my flow. So, this is what I'm going to execute now. So, I've created, previously I created Async as a single function that does this. But then, in order to do more, I made Async a class. And Async execute will actually execute the process. So, you can connect it to the loop. I will come to that why we need to connect it to the loop and all later on. For now, we can just run it. So, I could do this in the user group meeting time itself. But then, problem is when you are executing some asynchronous code, you don't know what is being actually executed. Right? So, if there is some way to debug and see what is actually happening, it will be better. Right? So, that is what I incorporated now. So, when we run, it will tell you what is happening. How this code is being interpreted. So, let me get to that demo. So, it is waiting for eight seconds. So, when one process is calling another process inside, that also you can see. So, the generator is internally calling a callback function. So, you can see that is happening there. Right? And the last two other results, basically like null because there is no error. It finished successfully. Boolean is the result of the execution of that function. So, if we compare this, let me go back to the presentation. So, let me do a line by line comparison. So, when you say yield two seconds as a string, it will await for two seconds in order to execute that. And then, when you say given RA of timer delay eight, it will execute timer delay of eight as a function. And basically like what I'm giving as a debug information is a pseudo code. It's not exactly the real code that is being executed, which is really very difficult. So, I want to give you a representation of what is happening so that you have an idea of what is happening. So, yeah. So, next timer hold, which is returning a generator, which is being executed. Internally, it's called timer wait. And then, there is a promise that is executed. And then, again, the return value is a generator. So, I'm processing that generator also through the flow. This one thing that I may change in the future. If at all we want to return a generator, which needs to be handled elsewhere, I can simply pass the generator there. So, whenever you need to wait for something, probably put a yield there, it will wait when it is through the engine. So, that's what I'm thinking. Okay. So, now, let us talk about the loop. Let us talk about cooperative multitasking. So, what is cooperative multitasking? How do we explain? Say, in a QI session, the organizer is holding the mic and whoever is raising the hand, he will go and pass the mic to them. They ask the question and it gets answered. And then, mic goes to the next person. That's how the whole thing happens. So, that can be, in a way, a cooperative multitasking. So, processor, a single thread of a processor can handle only one thing at a time. So, every task that is given to it, if it knows when it is free, like when it doesn't have to do anything, say, I'm waiting for a response from the server, so I give it back to you. And the next process, which is asking for it, it will pass it to that. And that way, by being aware and by cooperating, whenever you need not hold the processor, you give it to the next one. So, there will be some person who will be passing the mic to others. So, that role is played by the loop. Whenever something is free, it will just, loop will execute the next one. It will execute the next one. When everything is finished, it will start the new loop. Right? So, this is basically freeing up the processor load and giving the processor to different tasks. So, it is implemented using the loop interface. This is the interface of a loop. Basically, the interesting thing that you can see here is the future tick. So, it runs once. It finished all the tasks that needs to be served and then running a new loop. So, if you don't want to hold the processor, what you can do is, I have this work to do, but I'm giving it to you. Let's something else handle the processor. Later on, on the next loop, on the next frame, you execute this for me. So, that you can do by saying future tick. Similarly, there are data streams. Right? Add a read stream, write stream and all. So, whenever data is in, it will pay attention to that. So, when somebody is raising hand, when some data comes in. So, if you are writing a web server, everything in PHP, no engine X, no Apache and all, that time, what you can do is, whenever some request comes through a socket. Right? That means that process will, like, that task will raise a hand and then loop will pass the processor to it whenever it can. Once that is complete, it can give it back. So, this is the best way, like, this is the best optimization you can do to a single thread. Because, say, if you are in the restaurant business and you want to be very effective, the best way you can do that is by fine-tuning the performance of each and every individual. Right? So, their time has to be, like, best utilized. When they are baking pizza, if pizza is in the oven, they don't have to wait there. They can go make the smoothie and serve the client. Kind of, those optimizations are very important. That is the bottom most where you can fine-tune. You start there, then throw more people at it. Then only you will be able to get a very good operation. Just throwing people at it doesn't solve the problem. Right? That is something that we have seen. Too many cooks. Okay. So, let me take you through the source code. And before that, I can show you a complex process execution also. So, what I executed before is a very simple thing. Let me execute. So, this is a web server written in React PHP. So, basically what it does is, like, when it starts, it checks whether the blockchain is running. A node of the blockchain is available. If it is available, it will check whether a specific smart contract is deployed or not. If it couldn't find it, it will deploy and get the address of the smart contract, note it down. And then on that smart contract, if it is just created a new, it will add some, like, do some processes to set it up, right? And then start the web server. Then you'll be able to make requests and get the response and all. So, I'm just running that. Okay. So, I need to, like, reduce the size so that you'll be able to see it. Okay. So, now it's available, right? So, it is starting the server. Parallely, it is trying to load the contract. And internally, the load contract function is checking whether the specified address is a contract or not. How it does that, it basically asks Ethereum to get the code for that particular address. If it is a, if an address belongs to a smart contract, you will get the code for that. If it belongs to a person, there won't be any code. So, that is the logic used in the, that function. Anyway, I don't have to take you deeply into the smart contract accessing code and all. But that is what it internally does. So, all the function that it calls, you can see. And now, it hasn't finished. It is still running the server. So, I can make requests and it will respond back. So, let me see. So, I talked about Resler, right? It's an API framework. So, I'm writing a new version of Resler which can do asynchronous processes as a Resful API. So, you write a generator, part of a class method. That will be executed in an asynchronous fashion and whatever response you send will be out there. So, these are all the users available in the system. If I go back, you can see it is actually showing a start and stop. It didn't execute any asynchronous code. So, let me show you the root that is being executed as well. So, users is an API class which has an index function which is called when you just call users. So, here, if you see this, here I'm just returning the result because address is already captured in the system. If it is not captured, it will yield the start which will return the data later on. So, because it doesn't have to do anything asynchronous, it just returns immediately. There is no time that is taken. So, even conditional asynchronous operation can be done this way. So, let me go back to the code that I promised to show. Okay. Run does the most of the job. Basically, you give it a flow which is a generator function and then a callback. And the depth and all is used for the debug view that I'm rendering, which is basically a PSR logger interface. You can attach any logger to that. It will throw that output there. So, what it does is basically it tries to see whether that flow is still valid. There is still something to run. If it is not there, it will just get the return value. And if the return value is a generator, again, it will process it through that. It will execute that. Otherwise, if the callback is given, if it can be executed, it will just execute and give you the result. So, this is what is happening with that index function there. So, if not, if it is still valid, it will take the current value and then check whether the parameter that is passed for that particular at that yield statement, whatever comes after the yield, it takes that and that is the value here. So, it checks whether it is an array and how many items in the array. Based on that, it checks if the first item in the array itself is callable. Or if the first two items in the array are callable. That means remaining other parameters. It will separate them. And then after separation, if we get a callable function, it will call and it will add the callback function to that before that. And then when that callback function is called, if there is an error, it will immediately return. Instead of processing the whole thing, it will return. Otherwise, it will continue executing the flow. Basically, this is a recursive function. It will call itself many times to move the generator from one yield to another. That's how it will execute the whole flow. So, similarly, if it is a promise interface, it will await for the promise. It will just throw this information at the logger and it will execute that. If it is a generator, it will execute that. And if it is just a normal value, it will put place this value inside that function. So, whatever that reads before yield, where x is equal to yield so and so, that value will be placed there. So, that x will be the value of this function's result. And then the execution will continue. So, that's how it works. And I talked about the loop interface. And how we can pass the thing back to the manager, the loop, so that it can pass it to somebody else. So, that is what is happening here. So, this one, this class can take a loop. When a loop is given, what it does is it executes at a future tick. Every time it has to recursively execute, right? That time, that is an opportunity for it to pass back. So, if the loop exists, it will pass back so that other things can be executed and then it will come back here and we will continue the flow. So, that's how the whole system works. Okay. So, I will release the source code as soon as I get back to my desk and start pushing it. I don't want to do it live here. Any questions? Anything you didn't understand? Do I still have time or holding them from lunch? Okay. If I have time, I will show you a few things. A few asynchronous libraries. One is React PHP, which you can try. Another one is called Workman. And then, if you want to try the wrestler, the reactive version of wrestler, that is available here. So, that's all from me. Thank you. Thank you, Abu.