PHP code example of workerman / phpsocket.io

1. Go to this page and download the library: Download workerman/phpsocket.io library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

workerman / phpsocket.io example snippets




use Workerman\Worker;
use PHPSocketIO\SocketIO;

 SocketIO(2026);

$io->on('connection', function ($socket) use ($io) {
    $socket->on('chat message', function ($msg) use ($io) {
        $io->emit('chat message', $msg);
    });
});

Worker::runAll();



use Workerman\Worker;
use PHPSocketIO\SocketIO;

ion ($socket) {
    $socket->addedUser = false;

    // When the client emits 'new message', this listens and executes
    $socket->on('new message', function ($data) use ($socket) {
        $socket->broadcast->emit('new message', [
            'username' => $socket->username,
            'message'  => $data,
        ]);
    });

    // When the client emits 'add user', this listens and executes
    $socket->on('add user', function ($username) use ($socket) {
        // Note: global variables are used here for simplicity (demo only)
        global $usernames, $numUsers;

        $socket->username = $username;
        $usernames[$username] = $username;
        ++$numUsers;

        $socket->addedUser = true;
        $socket->emit('login', ['numUsers' => $numUsers]);

        $socket->broadcast->emit('user joined', [
            'username' => $socket->username,
            'numUsers' => $numUsers,
        ]);
    });

    // When the client emits 'typing', broadcast it to others
    $socket->on('typing', function () use ($socket) {
        $socket->broadcast->emit('typing', ['username' => $socket->username]);
    });

    // When the client emits 'stop typing', broadcast it to others
    $socket->on('stop typing', function () use ($socket) {
        $socket->broadcast->emit('stop typing', ['username' => $socket->username]);
    });

    // When the user disconnects
    $socket->on('disconnect', function () use ($socket) {
        global $usernames, $numUsers;

        if ($socket->addedUser) {
            unset($usernames[$socket->username]);
            --$numUsers;

            $socket->broadcast->emit('user left', [
                'username' => $socket->username,
                'numUsers' => $numUsers,
            ]);
        }
    });
});

Worker::runAll();



use Workerman\Worker;
use PHPSocketIO\SocketIO;

/your/path/of/server.pem',
        'local_pk'    => '/your/path/of/server.key',
        'verify_peer' => false,
    ],
];

$io = new SocketIO(2026, $context);

$io->on('connection', function ($socket) {
    echo "New connection: {$socket->id}\n";
});

Worker::runAll();



use Workerman\Worker;
use PHPSocketIO\SocketIO;

ion ($socket) {
    $socket->on('message with ack', function ($data, $callback) {
        if ($callback && is_callable($callback)) {
            $callback(0);
        }
    });
});

Worker::runAll();
bash
php start.php start       # debug mode
php start.php start -d    # daemon mode
php start.php stop        # stop
php start.php status      # status