PHPackages                             cydrickn/swoole-websocket-bundle - 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. [HTTP &amp; Networking](/categories/http)
4. /
5. cydrickn/swoole-websocket-bundle

ActiveSymfony-bundle[HTTP &amp; Networking](/categories/http)

cydrickn/swoole-websocket-bundle
================================

Swoole Websocket for Symfony

v1.0.0(3y ago)4512MITPHPPHP ^8.1

Since Aug 25Pushed 3y ago1 watchersCompare

[ Source](https://github.com/cydrickn/SwooleWebsocketBundle)[ Packagist](https://packagist.org/packages/cydrickn/swoole-websocket-bundle)[ RSS](/packages/cydrickn-swoole-websocket-bundle/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (2)Dependencies (3)Versions (3)Used By (0)

Swoole Websocket Bundle
=======================

[](#swoole-websocket-bundle)

A bundle for websocket using swoole

Requirements
------------

[](#requirements)

- [Swoole](https://www.swoole.com/) or [OpenSwoole](https://openswoole.com/)
- PHP &gt;= 8.1
- Symfony &gt;= 6.1

Intallation
-----------

[](#intallation)

Add the package

```
composer require cydrickn/swoole-websocket-bundle
```

Run the server
--------------

[](#run-the-server)

```
php ./bin/console websocket:server
```

Commands
--------

[](#commands)

### websocket:server

[](#websocketserver)

Run the websocket server

OptionsDetailsDefaulthostThe host of the server127.0.0.1portThe port of the server8000Events
------

[](#events)

This bundle will have an event where you can listen

### \\Cydrickn\\SwooleWebsocketBundle\\Event\\OpenEvent

[](#cydricknswoolewebsocketbundleeventopenevent)

This event will be trigger once a client have been connected to the server.

### \\Cydrickn\\SwooleWebsocketBundle\\Event\\MessageEvent

[](#cydricknswoolewebsocketbundleeventmessageevent)

This event will be trigger once the client send a message

### \\Cydrickn\\SwooleWebsocketBundle\\Event\\CloseEvent

[](#cydricknswoolewebsocketbundleeventcloseevent)

This event will be trigger once a client have been disconnected

Client connection using Javascript
----------------------------------

[](#client-connection-using-javascript)

For the basic way on connecting to websocket you can use the Browser Websocket

```
const socket = new WebSocket('ws://localhost:8080');

// Connection opened
socket.addEventListener('open', (event) => {
    socket.send('Hello Server!');
});

// Listen for messages
socket.addEventListener('message', (event) => {
    console.log('Message from server ', event.data);
});
```

Use it in runtime
-----------------

[](#use-it-in-runtime)

Using the command is fine, but if you happen you want to serve this using symfony runtime you can do so.

1. First you need to include symfony/runtime ```
    composer require symfony/runtime
    ```
2. Next run your `public/index.php` with env APP\_RUNTIME ```
    APP_RUNTIME=\\Cydrickn\\SwooleWebsocketBundle\\Runtime\\Runtime php ./public/index.php
    ```

#### Runtime Configuration

[](#runtime-configuration)

For runtime configuration you can add it to your `public/index.php`

KeyDetailsDefaulthostThe host of the server127.0.0.1portThe port of the server8000modeThe mode for server2 / SWOOLE\_PROCESSsock\_typeThe socket type for server1 / SWOOLE\_SOCK\_TCPsettingsThe setting is base on [swoole configuration ](https://openswoole.com/docs/modules/swoole-server/configuration)\[\] / Empty Arrayserve\_httpInclude to serve HTTPfalse**Example**:

```
#!/usr/bin/env php
