Websockets not connection when used function over the join method

Hi , Iam trying to add comments section in my web app , using phoenix.

The below method works fine.

socket.connect()

let channel = socket.channel(`comments:1`, {})
channel.join()
  .receive("ok", resp => { console.log("Joined successfully", resp) })
  .receive("error", resp => { console.log("Unable to join", resp) })
  
export default socket;

it gives me a joined succesfuly response in my console.but when i make the function like this…

socket.connect()
const createSocket = (topicId) => {
let channel = socket.channel(`comments:%{topicId}`, {})
channel.join()
  .receive("ok", resp => { console.log("Joined successfully", resp) })
  .receive("error", resp => { console.log("Unable to join", resp) })
}
window.createSocket=createSocket;

and i called in html file…

<script>
  window.createSocket(<%= @topic.id %>)
</script>

while compiling I got an error that websocket disconnected while handshaking…

I’m by far no JS expert, but shouldn’t this be `comments:${topicId}`?

1 Like

can you tell what should it be

I thought I did…

1 Like

What You missed is $ instead of % in Js interpolation.

1 Like