PHPackages                             votong/websocket-async - 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. votong/websocket-async

ActiveLibrary[HTTP &amp; Networking](/categories/http)

votong/websocket-async
======================

Laravel Websocket based on Ratchet, support pub/sub feature.

1.0.0(8y ago)17PHPPHP &gt;=5.4.0

Since Aug 28Pushed 8y ago1 watchersCompare

[ Source](https://github.com/votong/laravel-websocket-async)[ Packagist](https://packagist.org/packages/votong/websocket-async)[ RSS](/packages/votong-websocket-async/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (1)Versions (1)Used By (0)

Laravel WebSocket server
------------------------

[](#laravel-websocket-server)

> WebSocket server based on Ratchet. Built to be completely event driven so it can used in several different project without having to manipulate the base code.

Installation
------------

[](#installation)

Add `require` and `repositories` information in the projects `composer.json` file:

```
"require": {
        ...
        "freestream/websocket": "1.*"
        ...
    },
    "repositories": [
        ...
        {
            "type": "vcs",
            "url": "git@github.com:freestream/laravel-websocket.git"
        }
        ...
    ],
```

Now its time to run `composer update` in your terminal.

After the update is complete the service provider needs to be registered in `app/config/app.php` inside the `providers` array:

```
'Freestream\WebSocket\WebSocketServiceProvider',
```

Server side configuration
-------------------------

[](#server-side-configuration)

Run the following command in the projects root folder to startup the WebSocket server. By default the server will be run on port 8080 but by adding `--port=[number]` at the end of the command it is possible to change to any desired port.

```
php artisan websocket:start
```

This will startup a deamon service that will estsblish the WebSocket server. To make sure that command is constantly running it is recommended to use [Supervisord](http://supervisord.org/) to supervice the deamon.

Client side listener
--------------------

[](#client-side-listener)

This service comes included with the nessesary JavaScripts. To include these into the projects assets folder run the following command.

```
php artisan asset:publish freestream/web-socket
```

After that add thins line into the template file.

```

```

To estalish a WebSocket client add the following code.

```

    var webSocketClient = new WebSocketClient({
        prefix: 'Custom.Event.Prefix',
        server: 'localhost',
        port: '8080',
    });

```

The avilible configurations are:

```
debug       boolean     Enabled debug messages in browser console. Default is false.
prefix      string      Event firing prefix. Default 'Laravel.Freestream.WebSocket'
server      string      WebSocket server address. Default 'localhost'
port        integer     WebSocket server port. Default 8080
sessionId   string      Session ID for the opened WebSocket. Default random integer.
reconnect   boolean     Should reconnect automatically if losing connection. Default true.
```

Messages can be sent through the socket as soon as the connection is established. The first parameter is a event name that will be sent to the backend as a tracing event for easyer filtering. The second parameter is the message and can contain a string or a JSON.

```

    webSocketClient.message('event-name', 'This is my message');

```

Messages that are sent back from the server to the client contains a JSON with two elements, `origData` and `data`. OrigData contains any data that have been sent as a message and the server has responded to and data contains the reponse data from the server.

```
{
    origData: {
        ...
    },
    data: {
        ...
    },
}
```

Add Event Listensers to responed/listen to anything that happneds in the WebSocket.

```
document.addEventListener('Laravel.Freestream.WebSocket.Message.Received', function(event) {});
```

Events that will be fired is:

```
[PREFIX].Error
[PREFIX].Message.Received
[PREFIX].Connection.Established
[PREFIX].Connection.Closed
```

Server side listener
--------------------

[](#server-side-listener)

The server needs to be able to respond to any new connections or messages that are sent by the client. This is done by Laravels event listener. This can be setup in different ways but to recommented way is to use `events.php`.

If `events.php` is not already in the 'app/' folder create the file and after that open up 'app/start/global.php' and make sure the follwoing line is in the end of the file.

```
require app_path().'/events.php';
```

All events that will be handled by the server should now be placed inside `events.php`:

```
