Download the PHP package codememory/ws-server-bundle without Composer
On this page you can find all versions of the php package codememory/ws-server-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package ws-server-bundle
WebSocket Server Bundle
This pandle is a simple solution to raise a WebSocket Server in a matter of seconds, by default it is supported by Swoole as one of the most powerful frameworks
Install
Register this bundle if symfony flex didn't do it for you
Configuration
-
server:
- adapter: Service adapter if you decide to implement your own server. Default: "Swoole", Default Service: "WebSocketServerBundle::DEFAULT_SERVER_SERVICE"
- protocol: Server Protocol. Default: "websocket"
- host: Server Host. Default: "127.0.0.1"
- port: Server port. Default: "8079"
-
__event_listeners__: Set of message listeners
- { event: "TEST", listener: "App\WebSocketEventListeners\TestHandler" }: Example event listener
- config: Server configuration, depending on the server, the default is Swoole, so look at the swoole documentation. Default: "[]"
Default waiting message
An example implementation of an event listener handler
Registering an event listener
Events
-
codememory.ws_server.connection_open: Connection open
- class: Codememory\WebSocketServerBundle\Event\ConnectionOpenEvent
-
codememory.ws_server.connection_closed: Connection closed
- class: Codememory\WebSocketServerBundle\Event\ConnectionClosedEvent
-
__codememory.ws_server.message__: Received a new message from the connection
- class: Codememory\WebSocketServerBundle\Event\MessageEvent
-
__codememory.ws_server.message_handler_exception__: Exception handler during message listener call
- class: Codememory\WebSocketServerBundle\Event\MessageHandlerExceptionEvent
-
codememory.ws_server.message_sent: A message has been sent to the connection
- class: Codememory\WebSocketServerBundle\Event\MessageSentEvent
-
codememory.ws_server.start_server: The server starts up. Usually, in this event, the necessary child processes are added
- class: Codememory\WebSocketServerBundle\Event\StartServerEvent
- codememory.ws_server.start_worker: Server startup worker starts start
- class: Codememory\WebSocketServerBundle\Event\StartWorkerEvent
Let's implement the task of sending messages to specific users by their ID in the database
First, let's create a Listener for the Open Connection event and save all connections in redis
Now let's create an EventListener on the CONNECT message
Please note that in the current primer we will not use JWT, but will immediately pick up the user ID. In your example, instead of passing user_id, you can pass a JWT token, check its validity and get user_id from it
Now let's create a manager that will save messages to a queue that need to be sent to a specific user
And as a final step, we will add a process that will watch redis and check the existence of messages that need to be sent to the user
That's all, this example is not ideal and requires changes and depends on your needs
Now if we want to send a message to a user with ID 500, we just need to use our manager anywhere in the code and the sendMessage method
All versions of ws-server-bundle with dependencies
symfony/console Version 6.2.*
symfony/event-dispatcher Version 6.2.*
openswoole/core Version ^22.1
php Version >=8.1