Unable to receive broadcasted messages

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?

1 Like

you need

  channel.on("change", payload => {
    console.log(payload)
  });
1 Like

@outlog I have already got channel.on …sorry

1 Like

Can you then show how? The code you provided in the OP does not have channel.on("change", ...)

1 Like
channel.on("change", data => {
	   console.log("INSIDE THE CHANGE EVENT");
	    	
 })

if I change the topic to “room:*” it works fine

1 Like

so you are able to receive broadcasted messages.

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.

1 Like

@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.

1 Like

How does a list look like that you pass into your broadcasting function?

1 Like

@nobbz the payload looks like
payload = %{title: title,age: age}

1 Like

Not a payload to the broadcast but the list of recipients you pass into broadcast function and Mao over.

1 Like

@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”);

         })
1 Like

MyApp.Endpoint.broadcast_from!(self(), "room:tech", "change", payload)

Can you try this?

1 Like

thanks @amnu3387 I tried it but it doesn’t work

1 Like

Im at Home now and I do realize that I really shouldn’t try to answer here when I’m crowded in the Subway :rofl:

So can you please show us where you change fromroom:tech to room:* to make it work?

1 Like
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") 
}

I have changed every topic to “*”

1 Like

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?

1 Like

@nobbz now I am broadcasting to "room:*"

1 Like

I’ve not seen you are passing a strong literal there. I just saw the variable still being bound to tech…

Still I think that there are missing parts, can you please share a minimal project that can be used to reproduce your problem?

1 Like

@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

1 Like

Can you please provide a demo-repo that can be used to reproduce your error as well as a description of the steps necessary to see it?

1 Like