Let's quickly add a little check into our code to make that problem go away. In assets/js/socket.js, we're going to wrap our channel connection code inside of a conditional:
// Only connect to the socket if the polls channel actually exists!
if (document.getElementById('enable-polls-channel')) {
// Create a channel to handle joining/sending/receiving
const channel = socket.channel('polls:lobby', {});
// Next, join the topic on the channel!
channel
.join()
.receive('ok', res => console.log('Joined channel:', res))
.receive('error', res => console.log('Failed to join channel:', res));
}
If we refresh our browser on the homepage of our app we'll no longer see that 'Joined channel' message, which is good! If we browse over to /votes, however, we still don't get anything showing up, so we'll want to clear that up too. At the top of lib/vocial_web/templates/poll/index.html.eex, add the following line:
<i id="enable-polls-channel" />
Now when you refresh that page, you should see the same "Joined channel: {}" message in your developer console. We'll also need to add in something that will send a message, so we'll add a quick mostly useless button that will allow us to send a single message across our socket so that we can verify that functionality is working as expected.