Last active
August 22, 2016 06:45
-
-
Save joakimk/7b9ed5138c48594f0cdecfe95cb6c41e to your computer and use it in GitHub Desktop.
An example of auto reload on deploy in an Elixir Phoenix app
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Socket} from "phoenix" | |
let socket = new Socket("/socket", { params: {} }) | |
socket.connect() | |
let channel = socket.channel("updates", {}) | |
channel.join() | |
// Reload the app if it's out of date when it joins the websocket | |
let revision = null | |
channel.on("welcome", welcome => { | |
if(!revision) { revision = welcome.revision } | |
if(revision != welcome.revision) { window.location.reload() } | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyApp.UpdatesChannel do | |
use Phoenix.Channel | |
def join(_channel, _message, socket) do | |
send self, :after_join | |
{:ok, socket} | |
end | |
def handle_info(:after_join, socket) do | |
# HEROKU_SLUG_COMMIT is available through https://devcenter.heroku.com/articles/dyno-metadata | |
push socket, "welcome", %{ revision: System.get_env("HEROKU_SLUG_COMMIT") } | |
{:noreply, socket} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment