Skip to content

Instantly share code, notes, and snippets.

@joakimk
Last active August 22, 2016 06:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joakimk/7b9ed5138c48594f0cdecfe95cb6c41e to your computer and use it in GitHub Desktop.
Save joakimk/7b9ed5138c48594f0cdecfe95cb6c41e to your computer and use it in GitHub Desktop.
An example of auto reload on deploy in an Elixir Phoenix app
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() }
})
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