2
2
3
3
A service-based, Laravel PHP implementation of an async, realtime, WebSocket server.
4
4
5
+ ## Table of Contents
6
+
7
+ - [ Installation] ( #installation )
8
+ - [ Configure the Environment] ( #configure-the-environment )
9
+ - [ Nginx Websocket Proxy Configuration] ( #nginx-websocket-proxy-configuration )
10
+ - [ Running the Server (Supervisord)] ( #running-the-server )
11
+ - [ Usage Guide] ( #usage-guide )
12
+ - [ Extending the Server Manager] ( #extending-the-server-manager )
13
+ - [ Pushing Messages to the Realtime Queue] ( #pushing-messages-to-the-realtime-queue )
14
+ - [ Authenticating Client Messages] ( #authenticating-client-messages )
15
+ - [ Licensing] ( #licensing )
16
+
5
17
## Installation
6
18
7
19
The package installs into a Laravel application like any other Laravel package:
@@ -10,6 +22,14 @@ The package installs into a Laravel application like any other Laravel package:
10
22
composer require artisansdk/server ~1.0
11
23
```
12
24
25
+ Then in your Laravel application's ` config/app.php ` add the ` ArtisanSDK\Server\Provider::class `
26
+ to the ` providers ` key. This will register the configs and Artisan commands provided
27
+ by the package. You can publish these configs to ` config/server.php ` by running:
28
+
29
+ ```
30
+ php artisan vendor:publish --provider="ArtisanSDK\\Server\\Provider" --tag=config`
31
+ ```
32
+
13
33
> ** Show Me:** You can see how to integrate this package by browsing the source
14
34
code of [ ` larandomizer/app ` ] ( http://github.com/larandomizer/app ) which powers
15
35
[ Larandomizer.com] ( http://larandomizer.com ) .
@@ -28,16 +48,6 @@ for server customization:
28
48
- ` SERVER_QUEUE_DRIVER ` (` beanstalkd ` ): the driver to use for the realtime message queue
29
49
- ` SERVER_KEY ` (` password ` ): the admin password to authenticate connections against admin protected connections
30
50
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
51
### Nginx Websocket Proxy Configuration
42
52
43
53
Nginx makes the perfect lightweight frontend server for the Laravel backend
@@ -49,7 +59,7 @@ settings you can host websockets securely with the `wss://` protocol allowing
49
59
Nginx to handle the SSL connection and your websocket server handling basic HTTP.
50
60
51
61
```
52
- location /socket / {
62
+ location /server / {
53
63
proxy_pass http://127.0.0.1:8080;
54
64
proxy_set_header X-Real-IP $remote_addr;
55
65
proxy_set_header Host $host;
@@ -63,16 +73,14 @@ location /socket/ {
63
73
64
74
A quick note on the settings used:
65
75
66
- - ` location /socket / ` directs all traffic going to ` /socket / ` to the proxy
76
+ - ` location /server / ` directs all traffic going to ` /server / ` to the proxy
67
77
- ` proxy_pass ` passes the traffic to the localhost webserver on port ` 8080 `
68
78
- ` proxy_read_timeout ` customizes the connection drop to hang up idle connections
69
79
- ` proxy_http_version ` is the version of the websocket protocol in HTTP
70
80
- ` X-Real-IP ` header gives your websocket server the real IP of the client connection
71
81
- ` Upgrade ` and ` Connection ` headers instruct the browser to upgrade to websocket connection
72
82
73
- ## Usage Guide
74
-
75
- ### Starting the Server
83
+ ### Running the Server
76
84
77
85
The websocket server can be ran as an console command using ` php artisan server:start `
78
86
and if you pass ` --help ` to the command you can see additional options. You can
@@ -104,9 +112,56 @@ Forge does not add the `startsecs` by default but in practice this may be needed
104
112
to give the server ample time to start without hard exiting and forcing Supervisor
105
113
to give up on starting the process.
106
114
115
+
116
+ ## Usage Guide
117
+
118
+ ### Extending the Server Manager
119
+
120
+ The WebSocket server is a singleton instance that wraps brokers all connections
121
+ and messages between connected clients and the server via ` Broker ` . While this class
122
+ rarely needs modification, the broker collaborates with the ` Manager ` class. You
123
+ can think of the manager as the kernel of the application as it maintains the
124
+ initial boot state and event loop the entire time the server is running. It has
125
+ sensible defaults but will likely need extending for anything domain specific.
126
+
127
+ Simple create a new manager class in your local namespace such and include a ` boot() `
128
+ method which will be called to initialize your application's custom listeners:
129
+
130
+ ``` php
131
+ <?php
132
+
133
+ namespace App;
134
+
135
+ use ArtisanSDK\Server\Manager as BaseManager;
136
+
137
+ class Manager extends BaseManager
138
+ {
139
+ public function boot()
140
+ {
141
+ parent::boot();
142
+
143
+ $this->listener(...);
144
+ }
145
+ }
146
+ ```
147
+
148
+ As you can see in this example it's a good idea to call the parent ` boot() ` method
149
+ if you want to maintain the existing behavior and simply add on new behavior. With
150
+ the class extended, you now just need to update the configuration setting in
151
+ ` app/server.php ` under the key ` server.manager ` to ` App\Manager::class ` so the
152
+ server knows which manager to use:
153
+
154
+ ``` php
155
+ <?php
156
+
157
+ return [
158
+ 'manager' => App\Manager::class,
159
+ ];
160
+ ```
161
+
107
162
### Pushing Messages to the Realtime Queue
108
163
109
- By default the ` ArtisanSDK\Server\Manager@start () ` method adds a queue worker to the
164
+ By default the ` ArtisanSDK\Server\Manager@boot () ` method adds a queue worker to the
110
165
async event loop so that "offline" messages can be sent to the "realtime" connected
111
166
websocket clients. You can use any async driver (basically don't use ` sync ` as
112
167
the queue driver) but if you are using Laravel Forge it is pretty easy to use
@@ -118,9 +173,22 @@ to your "realtime" code you can `use ArtisanSDK\Server\Traits\WebsocketQueue` tr
118
173
your caller class and then call ` $this->queue(new Command) ` to push server
119
174
commands into the event loop of the websocket server. Commands should run nearly
120
175
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 () `
176
+ event loop. You can tweak the timing of the worker in ` ArtisanSDK\Server\Manager@boot () `
122
177
method's configuration of the worker.
123
178
179
+ ### Authenticating Client Messages
180
+
181
+ There is a basic auth scheme in place which allows the server to ` PromptForAuthentication `
182
+ against a connection and then remember that the connection is authenticated. This
183
+ simplifies further message processing and relies on any ` ClientMessage ` that must
184
+ be authenticated to implement the ` authorize() ` method. There are three basic
185
+ traits that can be used on any message to achieve a couple of common strategies:
186
+
187
+ - ` ArtisanSDK\Server\Traits\NoProtection ` : always returns true so allows any client to send the message
188
+ - ` ArtisanSDK\Server\Traits\ClientProtection ` : allows admin or specific connections to be authorized
189
+ - ` ArtisanSDK\Server\Traits\AdminProtection ` : allows only admins to be authorized to send the message
190
+
191
+
124
192
## Licensing
125
193
126
194
Copyright (c) 2017 [ Artisans Collaborative] ( http://artisanscollaborative.com )
0 commit comments