Skip to content

Commit 4ecb554

Browse files
committed
Update the placement of the section
Move the section to below the Putting it all together section according to suggestion by @josevalim
1 parent 6e00506 commit 4ecb554

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

guides/deployment/deployment.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,6 @@ As an example of deploying to other infrastructures, we also discuss four differ
1414

1515
Let's explore those steps above one by one.
1616

17-
#### Stickiness, Clustering, and the Long-Polling Transport {: .warning}
18-
19-
Phoenix supports two types of transports for its Socket implementation: WebSocket, and Long-Polling. When generating a Phoenix project, you can see the default configuration set in the generated `endpoint.ex` file:
20-
21-
```elixir
22-
socket "/live", Phoenix.LiveView.Socket,
23-
websocket: [connect_info: [session: @session_options]],
24-
longpoll: [connect_info: [session: @session_options]]
25-
```
26-
27-
This configuration tells Phoenix that both the WebSocket and the Long-Polling options are available, and based on the client's network conditions, Phoenix will first attempt to connect to the WebSocket, falling back to the Long-Poll option after the configured timeout found in the generated `app.js` file:
28-
29-
```javascript
30-
let liveSocket = new LiveSocket("/live", Socket, {
31-
longPollFallbackMs: 2500,
32-
params: {_csrf_token: csrfToken}
33-
})
34-
```
35-
36-
This automatic fallback comes with an important caveat. If you want Long-Polling to work properly, your application must either:
37-
38-
1. Utilize the Erlang VM's clustering capabilities, so the default `Phoenix.PubSub` adapter can broadcast messages across nodes
39-
40-
2. Choose a different `Phoenix.PubSub` adapter (such as `Phoenix.PubSub.Redis`)
41-
42-
3. Or your deployment option must implement sticky sessions - ensuring that all requests for a specific session go to the same machine
43-
44-
The reason for this is simple. While a WebSocket is a long-lived open connection to the same machine, long-polling works by opening a request to the server, waiting for a timeout or until the open request is fulfilled, and repeating this process. In order to preserve the state of the user's connected socket and to preserve the behaviour of a socket being long-lived, the user's process is kept alive, and each long-poll request attempts to find the user's stateful process. If the stateful process is not reachable, every request will create a new process and a new state, thereby breaking the fact that the socket is long-lived and stateful.
45-
4617
## Handling of your application secrets
4718

4819
All Phoenix applications have data that must be kept secure, for example, the username and password for your production database, and the secret Phoenix uses to sign and encrypt important information. The general recommendation is to keep those in environment variables and load them into your application. This is done in `config/runtime.exs` (formerly `config/prod.secret.exs` or `config/releases.exs`), which is responsible for loading secrets and configuration from environment variables.
@@ -144,6 +115,35 @@ And that's it. Next, you can use one of our official guides to deploy:
144115
* [to Fly.io](fly.html), a PaaS that deploys your servers close to your users with built-in distribution support
145116
* and [to Heroku](heroku.html), one of the most popular PaaS.
146117

118+
## Clustering and Long-Polling Transports
119+
120+
Phoenix supports two types of transports for its Socket implementation: WebSocket, and Long-Polling. When generating a Phoenix project, you can see the default configuration set in the generated `endpoint.ex` file:
121+
122+
```elixir
123+
socket "/live", Phoenix.LiveView.Socket,
124+
websocket: [connect_info: [session: @session_options]],
125+
longpoll: [connect_info: [session: @session_options]]
126+
```
127+
128+
This configuration tells Phoenix that both the WebSocket and the Long-Polling options are available, and based on the client's network conditions, Phoenix will first attempt to connect to the WebSocket, falling back to the Long-Poll option after the configured timeout found in the generated `app.js` file:
129+
130+
```javascript
131+
let liveSocket = new LiveSocket("/live", Socket, {
132+
longPollFallbackMs: 2500,
133+
params: {_csrf_token: csrfToken}
134+
})
135+
```
136+
137+
If you are running more than one machine in production, which is the recommended approach in most cases, this automatic fallback comes with an important caveat. If you want Long-Polling to work properly, your application must either:
138+
139+
1. Utilize the Erlang VM's clustering capabilities, so the default `Phoenix.PubSub` adapter can broadcast messages across nodes
140+
141+
2. Choose a different `Phoenix.PubSub` adapter (such as `Phoenix.PubSub.Redis`)
142+
143+
3. Or your deployment option must implement sticky sessions - ensuring that all requests for a specific session go to the same machine
144+
145+
The reason for this is simple. While a WebSocket is a long-lived open connection to the same machine, long-polling works by opening a request to the server, waiting for a timeout or until the open request is fulfilled, and repeating this process. In order to preserve the state of the user's connected socket and to preserve the behaviour of a socket being long-lived, the user's process is kept alive, and each long-poll request attempts to find the user's stateful process. If the stateful process is not reachable, every request will create a new process and a new state, thereby breaking the fact that the socket is long-lived and stateful.
146+
147147
## Community Deployment Guides
148148

149149
* [Render](https://render.com) has first class support for Phoenix applications. There are guides for hosting Phoenix with [Mix releases](https://render.com/docs/deploy-phoenix), [Distillery](https://render.com/docs/deploy-phoenix-distillery), and as a [Distributed Elixir Cluster](https://render.com/docs/deploy-elixir-cluster).

0 commit comments

Comments
 (0)