Skip to content

Instantly share code, notes, and snippets.

@harfangk
Created October 10, 2016 12:08
Show Gist options
  • Save harfangk/bf5919460ab40cac97736c4228632b82 to your computer and use it in GitHub Desktop.
Save harfangk/bf5919460ab40cac97736c4228632b82 to your computer and use it in GitHub Desktop.
defmodule Rumbl.VideoChannel do
use Rumbl.Web, :channel
def join("videos:" <> video_id, _params, socket) do
{:ok, assign(socket, :video_id, String.to_integer(video_id))}
end
def handle_info(:ping, socket) do
count = socket.assigns[:count] || 1
push socket, "ping", %{count: count}
{:noreply, assign(socket, :count, count + 1)}
end
end
import Player from "./player"
let Video = {
init(socket, element){ if(!element){ return }
let playerId = element.getAttribute("data-player-id")
let videoId = element.getAttribute("data-id")
socket.connect()
Player.init(element.id, playerId, () => {
this.onReady(videoId, socket)
})
},
onReady(videoId, socket){
let msgContainer = document.getElementById("msg-container")
let msgInput = document.getElementById("msg-input")
let postButton = document.getElementById("msg-submit")
let vidChannel = socket.channel("videos:" + videoId)
postButton.addEventListener("click", e => {
let payload = {body: msgInput.value, at: Player.getCurrentTime()}
vidChannel.push("new_annotation", payload)
.receive("error", e => console.log(e) )
msgInput.value = ""
})
vidChannel.on("ping", ({count}) => console.log("PING", count) )
vidChannel.join()
.receive("ok", resp => console.log("joined the video channel", resp) )
.receive("error", reason => console.log("join failed", reason) )
}
}
export default Video
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment