Honest Raj
Honest Raj

Reputation: 3

websocket not working while adding fuction over join method

I am new to phoenix programming while I'm learning I'm following Stephen graders udemy course.

While creating a channel to create a comment section, it works finely on first code but when I made a function, the join method is not working properly.

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

Upvotes: 0

Views: 60

Answers (1)

S.B
S.B

Reputation: 847

You probably need to use javascript string interpolation operator

`comments:${topicId}`

instead of

`comments:%{topicId}`

Note the $ instead of %

Upvotes: 1

Related Questions