|
| 1 | +# Server |
| 2 | + |
| 3 | +A service-based, Laravel PHP implementation of an async, realtime, WebSocket server. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +The package installs into a Laravel application like any other Laravel package: |
| 8 | + |
| 9 | +``` |
| 10 | +composer require artisansdk/server ~1.0 |
| 11 | +``` |
| 12 | + |
| 13 | +> **Show Me:** You can see how to integrate this package by browsing the source |
| 14 | +code of [`larandomizer/app`](http://github.com/larandomizer/app) which powers |
| 15 | +[Larandomizer.com](http://larandomizer.com). |
| 16 | + |
| 17 | +### Configure the Environment |
| 18 | + |
| 19 | +You will still want to edit the `.env` file to customize environment settings. |
| 20 | +Note that no database is used as all data is stored in memory on the server. |
| 21 | +Restarting the server will cause all data to be lost. Below are available options |
| 22 | +for server customization: |
| 23 | + |
| 24 | +- `SERVER_ADDRESS` (`127.0.0.1`): sets the address the server should bind to (`0.0.0.0` would be for allowing all external connections) |
| 25 | +- `SERVER_PORT` (`8080`): sets the port the server will listen on for websocket connections |
| 26 | +- `SERVER_MAX_CONNECTIONS` (`100`): the server rejects new connections after this limit (set to `0` to allow unlimited) |
| 27 | +- `SERVER_QUEUE` (`default`): the name of the queue that realtime messages will be sent to |
| 28 | +- `SERVER_QUEUE_DRIVER` (`beanstalkd`): the driver to use for the realtime message queue |
| 29 | +- `SERVER_KEY` (`password`): the admin password to authenticate connections against admin protected connections |
| 30 | + |
| 31 | +There is a basic auth scheme in place which allows the server to `PromptForAuthentication` |
| 32 | +against a connection and then remember that the connection is authenticated. This |
| 33 | +simplifies further message processing and relies on any `ClientMessage` that must |
| 34 | +be authenticated to implement the `authorize()` method. There are three basic |
| 35 | +traits that can be used on any message to achieve a couple of common strategies: |
| 36 | + |
| 37 | +- `ArtisanSDK\Server\Traits\NoProtection`: always returns true so allows any client to send the message |
| 38 | +- `ArtisanSDK\Server\Traits\ClientProtection`: allows admin connections and a specific related connection to be authorized |
| 39 | +- `ArtisanSDK\Server\Traits\AdminProtection`: allows only admins to be authorized to send the message |
| 40 | + |
| 41 | +### Nginx Websocket Proxy Configuration |
| 42 | + |
| 43 | +Nginx makes the perfect lightweight frontend server for the Laravel backend |
| 44 | +application. Additionally it can be used to proxy websockets connecting on port |
| 45 | +`80` to the `8080` default server socket. Doing so helps get around some firewall |
| 46 | +settings. The following should be placed just before your default `location` |
| 47 | +directive for the Laravel application itself (e.g.: Forge's default). Using these |
| 48 | +settings you can host websockets securely with the `wss://` protocol allowing |
| 49 | +Nginx to handle the SSL connection and your websocket server handling basic HTTP. |
| 50 | + |
| 51 | +``` |
| 52 | +location /socket/ { |
| 53 | + proxy_pass http://127.0.0.1:8080; |
| 54 | + proxy_set_header X-Real-IP $remote_addr; |
| 55 | + proxy_set_header Host $host; |
| 56 | + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; |
| 57 | + proxy_read_timeout 5m; |
| 58 | + proxy_http_version 1.1; |
| 59 | + proxy_set_header Upgrade $http_upgrade; |
| 60 | + proxy_set_header Connection "upgrade"; |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +A quick note on the settings used: |
| 65 | + |
| 66 | +- `location /socket/` directs all traffic going to `/socket/` to the proxy |
| 67 | +- `proxy_pass` passes the traffic to the localhost webserver on port `8080` |
| 68 | +- `proxy_read_timeout` customizes the connection drop to hang up idle connections |
| 69 | +- `proxy_http_version` is the version of the websocket protocol in HTTP |
| 70 | +- `X-Real-IP` header gives your websocket server the real IP of the client connection |
| 71 | +- `Upgrade` and `Connection` headers instruct the browser to upgrade to websocket connection |
| 72 | + |
| 73 | +## Usage Guide |
| 74 | + |
| 75 | +### Starting the Server |
| 76 | + |
| 77 | +The websocket server can be ran as an console command using `php artisan server:start` |
| 78 | +and if you pass `--help` to the command you can see additional options. You can |
| 79 | +stop the running server by killing the process with `CMD + C` (or `CTRL + C`). |
| 80 | + |
| 81 | +In production you would want to have Supervisor monitor the server and restart |
| 82 | +it if ever it crashes. The demo application has a "Restart Server" command which |
| 83 | +actually just stops the server and expects Supervisor to start it again automatically. |
| 84 | +If you are using Laravel Forge this is pretty easy to do by adding a New Deamon |
| 85 | +on the server with a configuration of: |
| 86 | + |
| 87 | +- Command: `/usr/bin/php /home/forge/default/artisan server:start` |
| 88 | +- User: `forge` |
| 89 | + |
| 90 | +The resulting Supervisor config might be: |
| 91 | + |
| 92 | +``` |
| 93 | +[program:server] |
| 94 | +command=/usr/bin/php /home/forge/default/artisan server:start |
| 95 | +autostart=true |
| 96 | +autorestart=true |
| 97 | +user=forge |
| 98 | +redirect_stderr=true |
| 99 | +startsecs=1 |
| 100 | +stdout_logfile=/home/forge/.forge/server.log |
| 101 | +``` |
| 102 | + |
| 103 | +Forge does not add the `startsecs` by default but in practice this may be needed |
| 104 | +to give the server ample time to start without hard exiting and forcing Supervisor |
| 105 | +to give up on starting the process. |
| 106 | + |
| 107 | +### Pushing Messages to the Realtime Queue |
| 108 | + |
| 109 | +By default the `ArtisanSDK\Server\Manager@start()` method adds a queue worker to the |
| 110 | +async event loop so that "offline" messages can be sent to the "realtime" connected |
| 111 | +websocket clients. You can use any async driver (basically don't use `sync` as |
| 112 | +the queue driver) but if you are using Laravel Forge it is pretty easy to use |
| 113 | +`beanstalkd` driver. Set `SERVER_QUEUE_DRIVER` and `SERVER_QUEUE` in your `.env` |
| 114 | +to configure the driver and queue name for your realtime messages. |
| 115 | + |
| 116 | +To send messages from your "offline" code (e.g.: controllers, repositories, etc.) |
| 117 | +to your "realtime" code you can `use ArtisanSDK\Server\Traits\WebsocketQueue` trait in |
| 118 | +your caller class and then call `$this->queue(new Command)` to push server |
| 119 | +commands into the event loop of the websocket server. Commands should run nearly |
| 120 | +instantly though there can be some lag depending on remaining commands within the |
| 121 | +event loop. You can tweak the timing of the worker in `ArtisanSDK\Server\Manager@start()` |
| 122 | +method's configuration of the worker. |
| 123 | + |
| 124 | +## Licensing |
| 125 | + |
| 126 | +Copyright (c) 2017 [Artisans Collaborative](http://artisanscollaborative.com) |
| 127 | + |
| 128 | +This package is released under the MIT license. Please see the LICENSE file |
| 129 | +distributed with every copy of the code for commercial licensing terms. |
0 commit comments