ghostzero / tmi

PHP Twitch Messaging Interface
MIT License
24 stars 8 forks source link

Testing Swoole #4

Closed ghostzero closed 1 year ago

ghostzero commented 3 years ago

I just found out that Swoole is a neat high performance extension. i should play with it.

https://www.swoole.co.uk

It's maybe a good replacement for react php?

mkarnicki commented 3 years ago

You have probably already heard of Laravel Octane that Taylor is cooking, but because I used Swoole PHP on my side project, I thought I would chime in here.

Swoole docs are not great, I have to admit. wiki.swoole.com is a Chinese version of docs, and contains more details than swoole.co.uk (I used Google Translate a bit). BUT... the performance is just nuts. So I just pushed through, I played with it some, and I currently use it to maintain a large number of concurrent websocket connections with Twitch PubSub.

Note that Swoole PHP is a PHP extension written in C. React and AmpPHP are PHP implementations of async. Swoole also supports coroutines, which means you can run multiple functions concurrently within a context of a single thread, and whenever one waits for I/O (network, disk, etc.), the scheduler will switch to another coroutine. And the best thing is... you don't even have to think about blocking I/O, coroutines handle this for you. It's like node.js, but better. More like Go, where from it was inspired with "goroutines".

So yes, I would encourage you to at least play with it. One day, you may want to switch your fabulous project (I only intend to start using it, but I'm excited, so thank you!) to use Swoole. And because performance is important, esp. in your tmi-cluster, Swoole will handle that traffic with no sweat.

I encourage you to have a look, or if you prefer, perhaps wait until Laravel Octane is out, if that encourages you more.

Note that Laravel was not created with stateful apps in mind. It uses some static methods or caching of initialized class members, so there are projects like swoole-laravel or laravel-s that try to address this. BUT, Laravel Octane will be a first-party integration of Swoole into Laravel, so I'm pretty excited to see how Taylor patched Laravel to address these types of issues.

Enojoy!

ghostzero commented 3 years ago

Wow, thanks for your insights. I already heard about Octane, that's sounds awesome. Swoole got my attention because of its a c extension and may sutes better as ReactPHP. That's why I want to invest time to test with it.

But I currently have problem to get Swoole running on my Notebook (Ubuntu 20.04) because if have no plan how to configure and compile it. Because it says curl is missing for example. But I've installed curl.

😅

mkarnicki commented 3 years ago

I think I installed it using pecl. When configuring, it asked if I want to install support for curl, json, etc. In any case, you should be able to test drive it using a Docker image, too. This way you won't have to clutter your notebook with Swoole and its dependencies :)

https://www.swoole.co.uk/docs/get-started/try-docker

https://github.com/swoole/docker-swoole

ghostzero commented 3 years ago

Thanks @mkarnicki! I tested the docker image, after few days Sury had also created a package for Swoole, so you can now just apt install php-swoole with his PPA.

I created a swoole branch, which implements the swoole extension. Currently there are tests missing, that checks if the event handler truely runs async. PR is #6.

mkarnicki commented 3 years ago

Sorry for late reply @ghostzero ! Glad you could give it a spin, and trying it for tmi :) I haven't had a chance to give TMI a good spin yet. Nevertheless, props for your work in the open 👏 !