News for hackers. Stuff that matters.
Re:Building a Distributed Turn-Based Game System in Elixir by keymone on Friday April 30, 2021 @06:23AM (#26991679)
Sorry, that’s not acknowledging that latency can become an issue, that’s acknowledging that using server-side rendering for things that don’t require a server isn’t the best of ideas (shocker, I know).
Re:Building a Distributed Turn-Based Game System in Elixir by sfusato on Friday April 30, 2021 @07:03AM (#26991928)
You would have latency in all apps that require a server round-trip regardless of the stack used.

When you need to go to the server, you go to the server. There's no other way around it.

I would be curious to hear how you solve this in other stacks? SPAs, whatever, when they need something from the server, they reach for the server.

Re:Building a Distributed Turn-Based Game System in Elixir by keymone on Friday April 30, 2021 @08:02AM (#26992315)
imagine an SPA for a basic CRUD system. there's a list view and details view with a delete button that returns you to the list.

in liveview server renders me the list view, i click details, server renders me details view, i click delete button, server renders me the list view.

if there's big latency/connection error/etc between clicking delete and getting back the rendered list - user just has to wait.

in spa i could optimistically assume that delete worked and render the list that i already have cached without the deleted item, allowing user to continue working immediately and if there was a disconnect/error - i could retry that delete in the background without bothering user, only prompting them after some number of retries.

don't see how could i implement this workflow in liveview.

Re:Building a Distributed Turn-Based Game System in Elixir by sfusato on Friday April 30, 2021 @08:25AM (#26992502)
You can do that in LiveView just as easily. Remove the item client side, then pushEvent to the server to handle the deletion. In case of any errors, notify the user, refresh the state .etc

pushEvent, pushEventTo (from client to server) [0]

push_event (from server to client) [1]

[0] https://hexdocs.pm/phoenix_live_view/js-interop.html#client-...

[1] https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#p...

Re:Building a Distributed Turn-Based Game System in Elixir by keymone on Friday April 30, 2021 @08:38AM (#26992618)
> Remove the item client side

so, "just use JS"?

> In case of any errors, notify the user, refresh the state .etc

so, "just use JS"?

every time you say "just use JS" you're diminishing the usecase of liveview because if i need so much js logic - why do i need to also use liveview if i can just use a framework/environment where i can share codebase between client and server seamlessly and have full control.

Re:Building a Distributed Turn-Based Game System in Elixir by sfusato on Friday April 30, 2021 @09:05AM (#26992878)
You stated don't see how could i implement this workflow in liveview.. I've presented you a way.

In case of any errors, notify the user, refresh the state .etc => This would all be done server-side and the client side would simply react automatically. The client side code in this case would have been minimal.

I don't think I'm diminishing anything. For quite a few years I was neck deep in React/Vue world. Now that I'm actively using LiveView, I can properly compare the differences between both approaches, cons and pros. For any new project, in the majority of cases I would pick Elixir with Phoenix LiveView instead of Elixir/Phoenix (backend) with React/Vue (frontend).

Re:Building a Distributed Turn-Based Game System in Elixir by keymone on Friday April 30, 2021 @09:31AM (#26993215)
You’ve presented a workaround and a hack tbh, not something natively supported because the workflow doesn’t map to liveview model, which is fine but you have to be honest with yourself and acknowledge when stuff like that happens, otherwise you’re in for lots of fun down the line.

> For any new project, in the majority of cases I would pick Elixir with Phoenix LiveView instead of Elixir/Phoenix (backend) with React/Vue (frontend).

This could just be recency bias. New tech is always exciting, old tech is always linked to memories of all the issues you’ve had in the past.

Re:Building a Distributed Turn-Based Game System in Elixir by sfusato on Friday April 30, 2021 @10:33AM (#26993970)
I wouldn't say it's a hack, but I would agree it's not the standard way to do things in the LiveView world, exactly because latency it's an overstated or misunderstood issue. But if you want to do more, LiveView gives you the tools.

> This could just be recency bias. New tech is always exciting, old tech is always linked to memories of all the issues you’ve had in the past.

I still maintain some React/Vue apps and work with on a daily basis, so it's not a distant memory.

I like choosing the right tool for the right job. For example, I would still choose ReactNative over 2 different code bases for a mobile app for a small team that needs to move fast. For the 5% of cases when that wouldn't do you would need to go native. I see the situation similarly with LiveView. It's hard to beat its productivity & power in 95% of use cases.

Re:Building a Distributed Turn-Based Game System in Elixir by diveanon on Friday April 30, 2021 @10:45AM (#26994104)
I did a deep dive into liveview and this was my take away.

Its nice tech, but once you start introducing JS again to improve UX you really start asking yourself why you didn't just build it with react in the first place.

Re:Building a Distributed Turn-Based Game System in Elixir by realusername on Friday April 30, 2021 @10:17AM (#26993764)
You could just delete the row with alpine or do a 3 line JS hook if you wanted to, it's quick and easy. That sounds a strange workflow though, it's generally better to make users wait for deletion.
Re:Building a Distributed Turn-Based Game System in Elixir by keymone on Friday April 30, 2021 @11:37AM (#26994830)
isn't it funny that when you're trying to praise tech you like, all sorts of examples jump into mind, but when you try criticizing something you like - all that imagination vanishes and all existing examples can be dismissed as strange :)
Re:Building a Distributed Turn-Based Game System in Elixir by realusername on Friday April 30, 2021 @11:40AM (#26994875)
I find it strange because that's not a behaviour I would use, but to each their own, it's the beauty of the web :).

If you really want to do it, you can add 3 lines on your project and that will work with any CRUD page you're building, I don't think that's unreasonable or difficult to do.

Edit:

Actually thinking about it, if you just made a form for that delete button and a phx-disable-with="" on the row it would probably work straight away without any JS hook.

Re:Building a Distributed Turn-Based Game System in Elixir by keymone on Friday April 30, 2021 @12:27PM (#26995578)
surely you recognize that there is a gap in functionality between liveview and fully fledged frameworks that provide more granular control over ui interactions?
Re:Building a Distributed Turn-Based Game System in Elixir by realusername on Friday April 30, 2021 @12:51PM (#26995939)
Like which other frameworks? You can code that example feature you pointed out quicker than in React if you want to. You have all the control you want in LiveView.

If you want a delete button per row, no code is needed and the phx-disable-with will work out of the box, if you want a global delete button on the top which deletes multiple rows front-end first before acknowledgement (with checkboxes + delete like in Gmail), 5 lines of JS maximum in a hook and you're set.

Re:Building a Distributed Turn-Based Game System in Elixir by keymone on Friday April 30, 2021 @01:00PM (#26996071)
> You have all the control you want in LiveView

that you can't even acknowledge that there is a gap in functionality between liveview, a fairly opinionated framework for server side rendering and fully fledged client-side frameworks tells me this is not going to be a productive conversation, so i'm out, bye

Re:Building a Distributed Turn-Based Game System in Elixir by realusername on Friday April 30, 2021 @01:29PM (#26996526)
Have you even used LiveView? It's not opinionated in any way, you can do whatever you want with it. It gives you extra features to remotely change pages but if you don't like having them you don't have to use them at all and can plug your favourite JS framework if you want to (or you can just use it for parts of the apps and not the rest if you want to).

I've worked for years with React and Angular and I don't really miss anything with a LiveView-based stack. LiveView features gives you 90% of what you want out of the box and for the rest it's fine having a bit of JS here and there to ensure a good experience.

Re:Building a Distributed Turn-Based Game System in Elixir by hnedeotes on Friday April 30, 2021 @06:56AM (#26991880)
What exactly are you thinking in terms of latency becoming an issue under LiveView but not on normal requests?

Do you mean when websites just need a full refresh because they lost their requests on some callback and no-one implemented recovery across the 5 levels of callbacks or something more specific?

Re:Building a Distributed Turn-Based Game System in Elixir by arjan_sch on Friday April 30, 2021 @07:23AM (#26992034)
Well with Liveview you go "full server state" for everything that you would normally just use plain JS for. For instance, toggling a checkbox or collapsing a div.

Having latency on such lowlevel interactions might make the UI feel sluggish as a whole.

Re:Building a Distributed Turn-Based Game System in Elixir by hnedeotes on Friday April 30, 2021 @08:04AM (#26992339)
Yeah certainly, but I'm not sure people are using it that way?

Most examples are to show cool stuff you can do, they're not production vetted. Like most JS examples out there don't really mean that people should be publishing live credentials with their bundles.

I imagine in most cases one would leave everything that is not behind a logged in status as normal routes/pages (signin, landing, contacts, etc). Or if not that those would be things requiring a socket/real-time interface anyway.

For the interactions, I don't think you even need to use alpine.js. Plain setup on DOMContentLoad, CSS Dropdowns/collapsibles that are replaced on JS load, proxying LiveView DOM/Morphdom events (if needed) so other components (even vue,react, etc) can listen to them, and CSS animations.

  import { setup_live } from "./phx/setup_live.js";
  import { setup_dropdowns } from "./interactivity/dropdowns.js";
  import { setup_collapsibles } from "./interactivity/collapsibles.js";
  import { setup_links } from "./interactivity/links.js";
  import { setup_inputs } from "./interactivity/inputs.js";

  function setup() {
      setup_live();
      setup_links();
      setup_dropdowns();
      setup_collapsibles();
      setup_inputs();
  }

  document.addEventListener("DOMContentLoaded", setup);


I went as far as having `onclick` handles and global window functions. Complete heresy. Yes, it's not 100% JS free, but it's pretty much low overhead.

Then LiveView is mostly for your admin dashboards and logged in users views, where it makes it pretty easy to do real-time feedback type of interactions/views and spa like navigation. Since you have proper auth and identification on the user, you can just log them off, rate-limit, block an account, and close their socket if needed.

Re:Building a Distributed Turn-Based Game System in Elixir by iudqnolq on Friday April 30, 2021 @12:12PM (#26995337)
Have you figured out any good way to have per-page javascript where the JavaScript is only sent over the wire for that pages?
Re:Building a Distributed Turn-Based Game System in Elixir by hnedeotes on Friday April 30, 2021 @01:15PM (#26996294)
From the top of my head not really, but this would depend on a few things:

- Is it a vendor lib ?

- It's not but is some particular file that is big enough to not make sense including on the root layout?

- It's neither, but functionality that can trigger multiple times and should be only once and only on those pages because it can conflict? or some variation of that?

I think they're all solvable but what makes sense will depend on those, but also on how you're using LiveView (is it LiveView only for logged-in users/some auth, can you set those on the live_view layout...)

But in some cases this is a problem also in spa's, where you have to use a snippet to check if the lib has been loaded, if not add a script tag to the body, or load it through js, etc...

Re:Building a Distributed Turn-Based Game System in Elixir by di4na on Friday April 30, 2021 @08:07AM (#26992362)
You don't have to. You can totally use JS for these in Liveview
Re:Building a Distributed Turn-Based Game System in Elixir by keymone on Friday April 30, 2021 @08:02AM (#26992320)