Giter Site home page Giter Site logo

swoole-by-examples's Introduction

Swoole by Examples

License: CC BY-NC-ND 4.0

The repository is to help developers to get familiar with Swoole through a variety of examples. All the examples are fully functioning; they can be executed and verified using the Docker images provided.

NOTE: I'm adding examples for latest versions of Swoole, so please be patient.

Setup the Development Environment

We use Docker to setup our development environment. Other than Docker, you don't need to install any other software to run and test the examples: you don't need to have PHP, Swoole, Composer, or some other software installed locally.

We use the official Docker image of Swoole to run the examples. There are tens of examples under repository swoole/docker-swoole shown how to use the image. Please spend some time checking it first.

Before running the examples, please run command docker-compose up -d under the root repository directory to start the Docker containers. There are two containers used to run the examples:

  • a server container where application servers are running.
  • a client container where client-side scripts should be executed.

Both containers have the same PHP scripts in place, so most standalone scripts (e.g., most CSP programming examples) can be executed from either container. Once the containers are running, you can use one of following commands to get a Bash shell in the containers:

docker compose exec -ti server bash # Get a Bash shell in the server container.
docker compose exec -ti client bash # Get a Bash shell in the client container.

List of Examples

swoole-by-examples's People

Contributors

berezuev avatar deminy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swoole-by-examples's Issues

How to use it in Controller

Hello, When I am using following code block in my laravel application controller it is throwing error.

<?php
 
 namespace App\Http\Controllers\Api;
 
 use App\Http\Controllers\Controller;
 use Illuminate\Http\Request;
 use Illuminate\Support\Facades\Artisan;
 use Swoole\Coroutine;
 
 use function Swoole\Coroutine\go;
 use function Swoole\Coroutine\run;
 
 class TestingController extends Controller
 {
     public function index(Request $request)
     {
         run(function () {
             for ($i = 0; $i < 2_000; $i++) {
                 go(function () {
                     // Note that we use the PHP function sleep() directly.
                     sleep(1);
                 });
             }
 
             // Note that there are 2_001 coroutines created, including the main coroutine created by function call run().
             echo count(Coroutine::listCoroutines()), " active coroutines when reaching the end of the PHP script.\n";
         });
 
         return response()->json([
             'message' => 'Api Working As Expected ' . $request->type,
             'ismobile' => isMobileRequest()
         ]);
     }
 }

And the error is like
image

Anyone can guide me how to use this code block inside Controller

Code sample to show how to use MySQL?

we are planning to use swoole to host a web api. we have seen this code:
https://www.swoole.co.uk/docs/modules/mysqli-connection-pool

$pool = new MysqliConfig())
            ->withHost(MYSQL_SERVER_HOST)
            ->withPort(MYSQL_SERVER_PORT)
            // ->withUnixSocket('/tmp/mysql.sock')
            ->withDbName(MYSQL_SERVER_DB)
            ->withCharset('utf8mb4')
            ->withUsername(MYSQL_SERVER_USER)
            ->withPassword(MYSQL_SERVER_PWD)
    )

some questions we have are:

  • where do we specify the number of connections in the pool?
  • and how is this pool available to the web server? I mean what should be the scope of the $pool variable?
    The swoole docs say not to use global variables but a connection pool by necessity is a global variable. Any code sample illustrating how to properly use a connection pool in a HTTP web server?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.