You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
app.listen(1234, () =>console.log("Listening on http://127.0.0.1:1234"));
62
62
```
63
63
64
-
IMPORTANT! Some extensions use the `onRequest`, `onUpgrade` and `onListen` hooks, that will not be fired in this scenario.
64
+
IMPORTANT! Some extensions use the `onRequest`, `onUpgrade` and `onListen` hooks, which will not be fired in this scenario.
65
+
66
+
### Hono
67
+
68
+
Hono is a modern web framework for multiple runtimes. It's a great fit for Hocuspocus as it supports the WebSocket protocol out of the box. Only when running in Node.js, does the Hono implementation requires a bit of extra code to support the WebSocket protocol.
IMPORTANT! Some extensions use the `onRequest`, `onUpgrade` and `onListen` hooks, that will not be fired in this scenario.
155
+
IMPORTANT! Some extensions use the `onRequest`, `onUpgrade` and `onListen` hooks, which will not be fired in this scenario.
105
156
106
157
### PHP / Laravel (Draft)
107
158
108
159
We've created a Laravel package to make integrating Laravel and Hocuspocus seamless.
109
160
110
161
You can find details about it here: [ueberdosis/hocuspocus-laravel](https://github.com/ueberdosis/hocuspocus-laravel)
111
162
112
-
113
163
The primary storage for Hocuspocus must be as a Y.Doc Uint8Array binary. At the moment, there are no compatible PHP libraries to read the YJS format therefore we have two options to access the data: save the data in a Laravel compatible format such as JSON _in addition_ to the primary storage, or create a separate nodejs server with an API to read the primary storage, parse the YJS format and return it to Laravel.
114
164
115
165
_Note: Do not be tempted to store the Y.Doc as JSON and recreate it as YJS binary when the user connects. This will cause issues with merging of updates and content will duplicate on new connections. The data must be stored as binary to make use of the YJS format._
116
166
117
167
#### Saving the data in primary storage
118
168
119
169
Use Laravels migration system to create a table to store the YJS binaries:
170
+
120
171
```
121
172
return new class extends Migration
122
173
{
@@ -135,6 +186,7 @@ return new class extends Migration
135
186
```
136
187
137
188
In the Hocuspocus server, you can use the dotenv library to retrieve the DB login details from `.env`:
189
+
138
190
```
139
191
import mysql from 'mysql2';
140
192
import dotenv from 'dotenv';
@@ -153,11 +205,12 @@ And then use the [database extension](/server/extensions/database) to store and
153
205
154
206
##### Option 1: Additionally storing the data in another format
155
207
156
-
Use the [webhook extension](/server/extensions/webhook) to send requests to Laravel when the document is updated, with the document in JSON format (see https://tiptap.dev/hocuspocus/guide/transformations#tiptap).
208
+
Use the [webhook extension](/server/extensions/webhook) to send requests to Laravel when the document is updated, with the document in JSON format (see <https://tiptap.dev/hocuspocus/guide/transformations#tiptap>).
157
209
158
210
##### Option 2: Retrieve the data on demand using a separate nodejs daemon (advanced)
159
211
160
212
Create a nodejs server using the http module:
213
+
161
214
```
162
215
const server = http.createServer(
163
216
...
@@ -171,6 +224,7 @@ Use the dotenv package as above to retrieve the mysql login details and perform
171
224
You can use the webhook extension for auth - rejecting the `onConnect` request will cause the Hocuspocus server to disconnect - however for security critical applications it is better to use a custom `onAuthenticate` hook as an attacker may be able to retrieve some data from the Hocuspocus server before The `onConnect` hooks are rejected.
172
225
173
226
To authenticate with the Laravel server we can use Laravel's built-in authentication system using the session cookie and a CSRF token. Add an onAuthenticate hook to your Hocuspocus server script which passes along the headers (and therefore the session cookie) and add the CSRF token to a request to the Laravel server:
227
+
174
228
```
175
229
const hocusServer = new Server({
176
230
...
@@ -194,13 +248,15 @@ const hocusServer = new Server({
194
248
```
195
249
196
250
And add a CSRF token to the request in the provider:
251
+
197
252
```
198
253
const provider = new HocuspocusProvider({
199
254
...
200
255
token: '{{ csrf_token() }}',
201
256
```
202
257
203
258
Finally, add a route in `api.php` to respond to the request. We can respond with an empty response and just use the request status to verify the authentication (i.e. status code 200 or 403). This example uses the built-in Laravel middleware to verify the session cookie and csrf token. You can add any further middleware here as needed such as `verified` or any custom middleware:
259
+
204
260
```
205
261
Route::middleware(['web', 'auth'])->get('/hocus', function (Request $request) {
0 commit comments