Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vj channel #10

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions web/channels/user_socket.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Chat.UserSocket do

channel "rooms:*", Chat.RoomChannel
channel "metadata", Chat.MetadataChannel
channel "vj", Chat.VjChannel
channel "notifications", Chat.NotificationChannel

def connect(_params, socket) do
Expand Down
37 changes: 37 additions & 0 deletions web/channels/vj_channel.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
defmodule Chat.VjChannel do
use Phoenix.Channel
require Logger

def join("vj", _message, socket) do
{:ok, pubsub} = Redix.PubSub.start_link(host: System.get_env("REDIS_HOST"), password: System.get_env("REDIS_PASSWORD"))

Redix.PubSub.subscribe(pubsub, "datafruits:vj", self())

{:ok, conn} = Redix.start_link(host: System.get_env("REDIS_HOST"), password: System.get_env("REDIS_PASSWORD"))
{:ok, vj_connected } = Redix.command(conn, ["GET", "datafruits:vj"])

send(self, {:after_join, vj_connected})
# push socket, "metadata", %{message: message}

{:ok, socket}
end

def handle_info({:after_join, vj_connected}, socket) do
push socket, "vj", %{vj_connected: vj_connected}
{:noreply, socket}
end

# Avoid throwing an error when a subscribed message enters the channel
def handle_info({:redix_pubsub, _redix_pid, _ref, :subscribed, _}, socket) do
{:noreply, socket}
end

# Handle the message coming from the Redis PubSub channel
def handle_info({:redix_pubsub, _redix_id, _ref, :message, %{channel: channel, payload: message}}, socket) do
Logger.debug "got message from pubsub #{message} on #{channel}"

push socket, "vj", %{message: message}

{:noreply, socket}
end
end