I am trying to understand how sockets and channels work.I have my user_socket.ex
defmodule MyApp.UserSocket do
use Phoenix.Socket
## Channels
channel “room:*”, MyApp.RoomChannel
transport :websocket, Phoenix.Transports.WebSocket
def connect(_params, socket) do
{:ok, socket}
end
def id(_socket), do: nil
end
And my room_channel.ex
defmodule MyApp.RoomChannel do
use Phoenix.Channel
def join("room:world",_payload,socket) do
{:ok, socket}
end
def join("room:"<>topic, payload, socket) do
resp = %{html: data}
{:ok, resp, socket}
end
def broadcast_change(list) do
room = "room:tech"
list |> Enum.each(fn(item) ->
payload = %{some: data}
MyApp.Endpoint.broadcast(room,"change", payload)
end)
end
end
And app.js
import "phoenix_html"
import socket from "./socket"
socket.connect()
let channel = socket.channel("room:world", {})
channel.join()
.receive("ok", resp => { console.log("Ready to receive news", resp) })
.receive("error", resp => { console.log("Unable to join", resp) })
$(somelink).click(function(){
topic="tech"
channel = socket.channel("room:"+topic)
channel.join()
.receive("ok", resp => { console.log("Ready to receive new items")})
.receive("error", resp => { console.log("Problem joining the channel")
}
Basically I am trying to broadcast messages but unable to receive any on the front end.The messages are being persisted in the databse.Could somebody please tell what I am doing wrong?
what is it that is not working? and what is the full code that isn’t doing what you expect?
issue seems to be with the js - both channels are named “channel” - one is locally assigned inside a function etc - but difficult to help (especially with js!) with only code fragments.
@outlog any topic other than the default “*” is not working.So if I do MyApp.Endpoint .broadcast("room:tech","change",payload) or MyApp.Endpoint.broadcast("room:bar","change",payload)
it doesn’t work.It’s one channel but two different topics.
@nobbz I am not broadcasting to any recipient but to the Endpoint on a particular topic.That topic could be"room:one","room:bar","room:play" etc.Anything other than "room:*" is not receiving any message on
channel.on(“change”, data => {
console.log(“INSIDE THE CHANGE EVENT”);
channel "room:*", MyApp.RoomChannel
transport :websocket, Phoenix.Transports.WebSocket
def connect(_params, socket) do
{:ok, socket}
end
def id(_socket), do: nil
end
room_channel.ex
defmodule MyApp.RoomChannel do
use Phoenix.Channel
def join("room:*",_payload,socket) do
{:ok, socket}
end
def join("room:"<>topic, payload, socket) do
resp = %{html: data}
{:ok, resp, socket}
end
def broadcast_change(list) do
room = "room:tech"
list |> Enum.each(fn(item) ->
payload = %{some: data}
MyApp.Endpoint.broadcast("room:*","change", payload)
end)
end
end
And app.js
import "phoenix_html"
import socket from "./socket"
socket.connect()
let channel = socket.channel("room:*", {})
channel.join()
.receive("ok", resp => { console.log("Ready to receive news", resp) })
.receive("error", resp => { console.log("Unable to join", resp) })
$(somelink).click(function(){
topic="tech"
channel = socket.channel("room:"+topic)
channel.join()
.receive("ok", resp => { console.log("Ready to receive new items")})
.receive("error", resp => { console.log("Problem joining the channel")
}
But you are not even sending broadcasts to that one … It looks really weird. I have the feeling that important parts of your code are missing as you didn’t even change the broadcast function…
Can you please provide a minimal project on GitHub, gitlab or similar to reproduce the problem?
@NobbZ I moved the broadcast_change function to another module and it works …getting handle_out function error now…which I written in the room_channel yet