I want to join a private channel only on a specific liveview page.
I’m using the phx-hook to run the channel.join when that specific area is mounted. But I needed a reference to the socket.
So I changed the generated user_socket.js a little bit to save the socket on the window object.
import { Socket } from "phoenix"
window.socket = new Socket("/socket", { params: { token: window.userToken } })
window.socket.connect();
Then in my hook chat.js file:
export const Chat = {
mounted() {
let someId = document.getElementById('someId').dataset.someId
const channel = window.socket.channel(`conversation:${someId}`, {});
channel.join()
.receive("ok", resp => { console.log("Joined successfully", resp) })
.receive("error", resp => { console.log("Unable to join", resp) })
channel.on('shout', (payload) => {
console.log("AYOOO")
});
},
};
My question:






















