diff --git a/.changeset/yellow-bulldogs-impress.md b/.changeset/yellow-bulldogs-impress.md new file mode 100644 index 0000000000..f60a5f607f --- /dev/null +++ b/.changeset/yellow-bulldogs-impress.md @@ -0,0 +1,5 @@ +--- +"docs": patch +--- + +Added info about pong reply to WebSocket ping event diff --git a/contributors.yml b/contributors.yml index 39008a694b..be10cd2d48 100644 --- a/contributors.yml +++ b/contributors.yml @@ -97,3 +97,4 @@ - u5r0 - wasimTQ - omahs +- codeit-ninja diff --git a/docs/guides/real-time/getting-started/websockets.md b/docs/guides/real-time/getting-started/websockets.md index dea32f55a2..7fd5b5f5cf 100644 --- a/docs/guides/real-time/getting-started/websockets.md +++ b/docs/guides/real-time/getting-started/websockets.md @@ -183,6 +183,23 @@ You may have noticed that, periodically, you will receive a message with a type application technology stack. 2. To verify that the connection is still active. +In order to prevent the connection from _closing_, you may reply with a `pong` event: + +```js +// Exemplary code +connection.addEventListener('message', (message) => { + const data = JSON.parse(message.data); + + if (data.type === 'ping') { + this.connection.send( + JSON.stringify({ + type: 'pong', + }), + ); + } +}); +``` + On Directus Cloud, this feature is enabled. If you are self-hosting, you can alter this behavior with the `WEBSOCKETS_HEARTBEAT_ENABLED` and `WEBSOCKETS_HEARTBEAT_PERIOD` environment variables.