I wanted to organize the use of socket and Presence in a template in my own way.
As a newbie, it seems to me that putting all the client code that deals with channels in sockets.js can convert it into a very large piece.
Especially if there are many pages that use sockets / Presence.
I do not know which is the best solution (or if this is a problem for other people), but my misunderstood functioning of js, leads me to organize the code well.
In socket.js
import {Socket, Presence} from "phoenix"
// Other stuff
export {
Presence,
socket
}
In app.js, instead of import socket from "./socket" at the end, I write
import {Presence, socket} from "./socket"
window.getPhoenixSocket = function () {
return socket;
};
window.getPhoenixPresence = function () {
return Presence;
};
And in the template eex (I know itâs not good practice to mix html and scripts) (except for the React guys!) I write the following script:
<script>
window.onload = function () {
let channel = getPhoenixSocket (). channel ("cute: channel", {})
let Presence = getPhoenixPresence ();
....
// Here I can use channel and Presence for my obscure purposes
....
}
</script>
Thus the code of each page that uses socket remains in the own page and not in socket.js
As I am not entirely satisfied with this solution, I ask you, is this correct? Is not it? What would be the best way to do it?
Is there someone who has this problem?
The issue is that you are mixing JavaScript styles. When you see import, you are looking at modular JavaScript. For browsers modular JavaScript is processed by bundlers like Brunch or Webpack. See Modern JavaScript Explained For Dinosaurs.
Ok, Alright, I mix styles (I do not know which). But the easy and simple solution, what is it? How would you put the code to be compatible with Brunch, easy and understandable?
Thanks
because it isnât optimal for pages that make significant use of JavaScript.
I posted a quick example here of how to convert to a modular style. In your case there is the additional challenge that you will likely need a separate bundle for each page - something that is handled by bundlers with âmultiple entry pointsâ, i.e. one bundle per entry point. There is a (Brunch) discussion about that:
However note that Phoenix 1.4 uses Webpack 4 instead of Brunch.
Technically the code that you put inside the script tags is supposed to be at the end of app.js (the page bundle) - in which case you wouldnât need to attach getPhoenixSocket and getPhoenixPresence to the global window object.
So your challenge is to get Brunch to generate the app1.js, app2.js, etc. bundles that you need for your various pages and include them in the page templates (and remove app.js from the layout template app.html.eex).