When running the `loop()` method via web, MadelineProto will automatically enable a **magical self-restart hack** (callback ID `restarter`), to keep the bot running even on webhosts with limited execution time.
Locking will also be handled automatically (as well as disconnection from the user that opened the page), so even if you start the script via web several times, only one instance will be running at a time (no need to do flocking manually!).
It relies on the shutdown function, so you must not set a custom shutdown function in your code, and instead use the **MadelineProto shutdown static API**:
```php
use danog\MadelineProto\Shutdown;
$id = Shutdown::addCallback(static function () {
// This function will run on shutdown
});
$id = Shutdown::addCallback(static function () {
// This function will run on shutdown
}, 'custom id');
$id = Shutdown::addCallback(static function () {
// This function will overwrite the previously set function with custom id
}, 'custom id');
$ok = Shutdown::removeCallback($id);
```
You can of course pass non-static functions, any type of callable is accepted.
A second optional parameter can also be accepted, containing the ID of the callable: you can use this if you want to later overwrite the callable with another callback, or remove it altogether.
The `removeCallback` will return true if the callback exists and it was removed correctly, false otherwise (as with all new MadelineProto 4.0 APIs, there are PHPDOCs for these methods so you'll see them in your IDE).
This will create an event handler class `MyEventHandler`, create a MadelineProto session, and set the event handler class to our newly created event handler.
The **new**`startAndLoop` method automatically initializes MadelineProto, **enables async**, logs in the user/bot, initializes error reporting, catches and reports all errors surfacing from the event loop to the peers returned by the `getReportPeers` method.
This yield syntax might be new to you, even if you already used MadelineProto in the past.
It's a new syntax to allow async **parallel processing** of updates and HUGE speed improvements.
It was recently introduced in MadelineProto, [here's a full explanation](ASYNC.html).
If your code still relies on the old synchronous behaviour, it's still supported, but I HIGHLY recommend you switch to the new async syntax: it's __super__ easy, [just add a `yield` in front of method calls](ASYNC.html)!
When an [Update](https://docs.madelineproto.xyz/API_docs/types/Update.html) is received, the corresponding `onUpdateType` event handler method is called.
To get a list of all possible update types, [click here](https://docs.madelineproto.xyz/API_docs/types/Update.html).
The `onLoop` method is not recommended anymore, use AMPHP's [repeat](https://amphp.org/amp/event-loop/api#repeat) or MadelineProto's [async loop API](ASYNC.html#async-loop-apis) to schedule actions in a cron-like manner.
If you intend to use your own constructor in the event handler, make sure to call the parent construtor with the only parameter provided to your constructor.
The update handling loop is started by the `$MadelineProto->loop()` method, and it will automatically restart the script if execution time runs out.
This will create an event handler class `EventHandler`, create a **combined** MadelineProto session with session files `bot.madeline`, `user.madeline`, `user2.madeline`, and set the event handler class to our newly created event handler.
Usage is the same as for [the normal event handler](#async-event-driven), with the difference that by using `loopFork`, multiple accounts can receive and handle updates in parallel, each with its own event handler instance.
$MadelineProto = new \danog\MadelineProto\API('bot.madeline');
$MadelineProto->start();
$MadelineProto->setNoop();
```
When an [Update](https://docs.madelineproto.xyz/API_docs/types/Update.html) is received, nothing is done. This is useful if you need to populate the internal peer database with peers to avoid `This peer is not present in the internal peer database errors`, but don't need to handle updates.