Skip to Content
Phoenix Web Development
book

Phoenix Web Development

by Brandon Richey
April 2018
Beginner to intermediate content levelBeginner to intermediate
406 pages
9h 33m
English
Packt Publishing
Content preview from Phoenix Web Development

Conditionally loading our socket

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

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Phoenix in Action

Phoenix in Action

Geoffrey Lessel
Real-Time Phoenix

Real-Time Phoenix

Stephen Bussey

Publisher Resources

ISBN: 9781787284197Supplemental Content