PHPackages                             bang/phpsocket.io - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. bang/phpsocket.io

ActiveLibrary[Queues &amp; Workers](/categories/queues)

bang/phpsocket.io
=================

A server side alternative implementation of socket.io in PHP8 based on Workerman

1.0(1y ago)014MITPHP

Since Jul 30Pushed 1y ago1 watchersCompare

[ Source](https://github.com/TTC1002335644/PHPSocket.io)[ Packagist](https://packagist.org/packages/bang/phpsocket.io)[ Docs](https://www.workerman.net)[ RSS](/packages/bang-phpsocketio/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (3)Versions (2)Used By (0)

phpsocket.io
============

[](#phpsocketio)

A server side alternative implementation of [socket.io](https://github.com/socketio/socket.io) in PHP based on [Workerman](https://github.com/walkor/Workerman).

Notice
======

[](#notice)

这个项目是fork `workerman/phpsocket.io`,因为`workerman/phpsocket.io`在`php8`上运行会出错，所以fork并发布一个支持在`php8`上可以正常运行的版本
support php &gt;= 8.0
Only support socket.io &gt;= v1.3.0 and &lt;= v2.x
This project is just translate socket.io by [workerman](https://github.com/walkor/Workerman).
More api just see

Install
=======

[](#install)

composer require bang/phpsocket.io

Examples
========

[](#examples)

Simple chat
-----------

[](#simple-chat)

start.php

```
use Workerman\Worker;
use PHPSocketIO\SocketIO;
require_once __DIR__ . '/vendor/autoload.php';

// Listen port 2021 for socket.io client
$io = new SocketIO(2021);
$io->on('connection', function ($socket) use ($io) {
    $socket->on('chat message', function ($msg) use ($io) {
        $io->emit('chat message', $msg);
    });
});

Worker::runAll();
```

Another chat demo
-----------------

[](#another-chat-demo)

[https://github.com/walkor/phpsocket.io/blob/master/examples/chat/start\_io.php](https://github.com/walkor/phpsocket.io/blob/master/examples/chat/start_io.php)

```
use Workerman\Worker;
use PHPSocketIO\SocketIO;
require_once __DIR__ . '/vendor/autoload.php';

// Listen port 2020 for socket.io client
$io = new SocketIO(2020);
$io->on('connection', function ($socket) {
    $socket->addedUser = false;

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

    // When the client emits 'add user', this listens and executes
    $socket->on('add user', function ($username) use ($socket) {
        global $usernames, $numUsers;

        // We store the username in the socket session for this client
        $socket->username = $username;
        // Add the client's username to the global list
        $usernames[$username] = $username;
        ++$numUsers;

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

        // echo globally (all clients) that a person has connected
        $socket->broadcast->emit('user joined', array(
            'username' => $socket->username,
            'numUsers' => $numUsers
        ));
    });

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

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

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

        // Remove the username from global usernames list
        if ($socket->addedUser) {
            unset($usernames[$socket->username]);
            --$numUsers;

            // echo globally that this client has left
            $socket->broadcast->emit('user left', array(
               'username' => $socket->username,
               'numUsers' => $numUsers
            ));
        }
   });
});

Worker::runAll();
```

Enable SSL for https
--------------------

[](#enable-ssl-for-https)

**`(phpsocket.io>=1.1.1 && workerman>=3.3.7 required)`**

start.php

```
