News for hackers. Stuff that matters.
Re:Data fetching on the web still sucks by heipei on Wednesday January 27, 2021 @01:21PM (#25931768)
This post feels like a uninformed and undifferentiated rant against "things are too complex". Let's start with the first paragraph: What does the JavaScript fetch API have to do with data management? How can you compare the fetch() API with Swagger (an API documentation format) with Protobuf (a serialisation format)? That doesn't even make sense.

Second paragraph: "The UI should automatically update everywhere the data is used". Again, what does this have to do with any of the above? That is state management, yeah, and you can build proper state management with any HTTP library and any message serialisation format.

Request batching: How would that happen "automatically"? By waiting to fire requests and then batching them?

UX when fetching data: What does that have to do with any of the above? You still have to decide how your UI displays the fact that a piece of data is loading. What do you expect there to be in place? Best thing I could imagine is to have a global operations indicators a la Adobe Lightroom which tells you how many HTTP requests are in flight.

I could go on, but the last paragraphs maybe highlights the lack of understanding the author had: "UI Frameworks (at this point, React has won)". If React had "won" then why would we be having this discussion. React hasn't "won" because it solves one piece of the puzzle: Rendering. For every little other thing you have to incorporate another library or figure out your own solution: Routing, State Management, CRUD / HTTP API, etc. If anything, Ember.js would most closely fit the bill of incorporating most of the things the author seems to care about yet can't articulate clearly.

Re:Data fetching on the web still sucks by bcherny on Wednesday January 27, 2021 @01:48PM (#25932166)
Author here.

Data fetching, caching, consistency, and UX are all closely related. If you treat them as separate problems, you're punting the problem onto product engineers, who won't solve it well. (See the last paragraph of the post, which suggests this same idea.)

> That is state management, yeah, and you can build proper state management with any HTTP library and any message serialisation format.

You're right that to do this manually it's just state management. But to automatically update the UI, it means your client data layer (eg. Apollo) needs to know & track the identity of fields; the data layer also needs to be able to subscribe to fetches anywhere in your app, not local fetches; it also means your protocol needs to support this identity (via agreed-upon ID fields); etc. These problems are all closely related.

> Request batching: How would that happen "automatically"? By waiting to fire requests and then batching them?

eg. Relay does this by statically combining requests throughout a React tree into a single request at the top of the tree. You could also do it dynamically, as you suggest. The tradeoff is often performance.

> UX when fetching data

Having engineers manually define loading states doesn't scale. React is approaching this problem with Suspense, and you could imagine standard loading states when fetches are in flight.

Re:Data fetching on the web still sucks by austincheney on Thursday January 28, 2021 @05:03AM (#25940116)
State management, for anybody half competent, is stupid easy and not tied to the UI. State management is a data storage/retrieval problem only. Once the UI has the state information it needs you can populate it for the user however you want in many different ways.

State management is also proclaimed as something it isn’t because web UI is full of incompetent expert beginners who can’t write two lines of original code and are helplessly mortified if their colossal framework is taken away.

Here is simple web UI state management:

* have a centrally available object in the UI code that stores state data.

* store that data upon change. That storage can be localStorage for maximum simplicity. It can also be a locally written file if you have a local running service or an http server. The further away from the local computer that storage location becomes the slower it is regardless of access mechanism.

* state changes can occur from user interaction or system updates to remote data. Most important are user interactions because that data is locally available. Keep up with remote system changes as best you can, but unless you own that data as well it’s a window into some distant concern.

* when state changes update your central state object save the changed state object. Most of the time UI developers are only concerned with state updates and not saving state because they don’t know what they are doing and hope the framework does everything (or it must obviously be unnecessary).

* once the state object is updated, and optionally saved, update the UI. For most UI developers updating the UI is the ultimate and only struggle. This is such a junior level basic required skill for UI developers. Knowing that you can root out the incompetent people during hiring.

But but but what about 2 way data binding... Treat it as an event. Update the central state store. Process the change. Still simple and no framework is needed.

Re:Data fetching on the web still sucks by richeyryan on Thursday January 28, 2021 @09:33AM (#25942261)
I'm curious about your approach to making UIs. Are there any libraries you do use?

Do you use anything to render your view or do you construct the nodes and insert them into the DOM directly?

Is your centrally available object an instance of a class or is it a plain JavaScript object?

Does it manage the state for all your UI or is it segmented in some way?

How do you drive changes to your UI? Is there an imperative process that renders the UI when you know the data model has changed or is your state object an observable or something that your views subscribe to?

Re:Data fetching on the web still sucks by austincheney on Thursday January 28, 2021 @11:23AM (#25944314)
I use the ws library for web sockets.

The state object is just a regular JavaScript object. I prefer to avoid classes as they increase complexity for almost no substantive benefit.

Changes to the UI are handled as events whether those events are from user interactions, HTTP responses, or web socket data events.

I update the DOM using old school DOM methods and some custom DOM methods I have written to do things the standards do not provide.

This stuff that people cry tears of blood over is easy and very basic. I really think the bane of evil is addEventListener allowing multiple handlers for the same events which allows laziness and super increased complexity.

What’s more challenging is writing test automation that executes in the browser and coordinates between browsers of different computers.

Re:Data fetching on the web still sucks by richeyryan on Thursday January 28, 2021 @11:43AM (#25944592)
Would the stuff you have on Github be a fair representation of your ideal way of writing a web UI?
Re:Data fetching on the web still sucks by austincheney on Thursday January 28, 2021 @11:56AM (#25944792)
Try this: https://github.com/prettydiff/share-file-systems/tree/tests

It is a complete GUI with full state management and the browser part of the application is very tiny.

Re:Data fetching on the web still sucks by richeyryan on Thursday January 28, 2021 @12:04PM (#25944924)
I had a look through that alright. Do you code in that style in work?
Re:Data fetching on the web still sucks by austincheney on Thursday January 28, 2021 @12:29PM (#25945276)
I have been away from my primary employer for about a year and they are HUGE with many different teams that do many different things in many different ways. I just try to gel to the team and not be disruptive.
Re:Data fetching on the web still sucks by nullvoxpopuli on Thursday January 28, 2021 @06:58AM (#25940778)
Oh,dear. Little too much redux koolaid, eh?
Re:Data fetching on the web still sucks by Garlef on Wednesday January 27, 2021 @02:28PM (#25932707)
I think the post is a bit unfortunate in its wording and this seems to have sent you off on a wrong track.

From how I read it, this the post is not specifically about JS and a discussion of the specific technologies mentioned but rather concerned with the following very general situation:

1. There's a user sitting in front of a browser.

2. There's a backend server providing data and points of interaction with that data.

I think the central point of the post now is that we don't have a satisfying technical solution for this situation.

Let's take a look at one of the points you mentioned. Maybe you might find there's actually some valid points in the post and give it a more favourable reread.

> UX when fetching data: What does that have to do with any of the above?

Here, the author of the post writes: "It’s a big burden for engineers to have to manually add loading spinners and error states, and engineers often forget."

I think it's more or less clear how you could implement this using only plain vanilla js. Cumbersome but doable: A very manual, imperative process.

Now let's envision a technology from a possible future:

    const twitterFeed = createMagicDataSource("https://twitter.com/...")

    const feedComponent = magicRendererComponent(twitterFeed, state => /* HTML like declarative description of the visuals */)

Imagine this was everything you had write in your code to get the following:

* state gets automatically loaded when the first instance of the component is created

* updates are automatically visualized in the client based on the internals of the declaration in the renderer

* you don't have to specify if the updates are done by polling, websockets or whatever: the two magic methods figure this out by themselves.

* you don't have to specify how the data is fetched in the first place. giving the a URI to the `createMagicDataSource` function is enough.

* additional instances of the component don't fetch the data again

* updates are efficient: the sync method only exchanges exactly the data required, only the minimal visual updates are performed

* marking feeds as "seen" by the user is also done by magic and syncs across devices (same for other non-ephemeral ui state).

Now: And I'm sure you'd agree that we are not there yet technologically. But I hope you agree that this would be really nice.

Re:Data fetching on the web still sucks by ivanhoe on Wednesday January 27, 2021 @05:35PM (#25935065)
> But I hope you agree that this would be really nice.

Black boxes with tons of magic are great if you need to do exactly that one thing that creators had in mind. When you need it to work a little bit different you're usually either just out of luck, or the added flexibility makes whole api very complex (and usually buggy, as it goes hand in hand). It's just super hard to make things very high-level and simple, while still flexible enough.

Having a number of simpler functionalities that you're free to compose any way you like is for me preferred approach anytime. Most of the things that you mention can be solved with fairly simple wrapper libs doing just one thing (tracking state of connection, tracking errors, batching requests, etc.). Perhaps not in a perfectly reusable way, but good enough to allow easy recycling from project to project, and it's way easier to use and debug and adjust than if we had one huge built-in service object to handle it all.

Re:Data fetching on the web still sucks by Garlef on Thursday January 28, 2021 @07:26AM (#25940971)
> Black boxes

There are a few black boxes most people are very happy not to peek into; Having these black boxes is a huge productivity win:

* Compilers

* Garbage Collection

* OS Kernels

* File systems

* Docker

* App Stores / package managers

* Infrastructure as code frameworks (i.e. terraform)

* JS Frontend frameworks

I think there's three things at work here: 1. Familarity with treating item X as a black box (High for compilers, low for infrastructure as code). 2. Maturity of the interfaces (High for compilers, low for infrastructure as code). 3. Relevance of the details for your use case / business context.

> Having a number of simpler functionalities that you're free to compose any way you like is for me preferred approach anytime. Most of the things that you mention can be solved with fairly simple wrapper libs doing just one thing

I totally agree: That's how I imagine how the magic functions are defined.

> Black boxes with tons of magic are great if you need to do exactly that one thing that creators had in mind.

This totally depends what your business context is. For most companies, dealing with CORS in HTTP requests is very far from their business domain. Still, developers spend a lot of time configuring and building endpoints, API clients, etc. at a low level of abstraction.

Re:Data fetching on the web still sucks by mewpmewp2 on Wednesday January 27, 2021 @03:55PM (#25933798)
I think parent is completely missing the point. In my opinion the way it should be is that in the backend you export a typed function like "getTasks(filter: Filter): Tasks

And in the frontend component you should be able to use it immediately and it would be correctly typed without having to duplicate types or anything. And if function is not complete yet it will not render the component before.

This should be instead of REST and Graphql, both of which have many issues imo.

Re:Data fetching on the web still sucks by sk5t on Wednesday January 27, 2021 @11:33PM (#25938217)
So backend and frontend must have a wholly compatible type system? Even good old WSDL only got 90% of the way there. Keep going with fancier approaches and you end up looking at CORBA and its ilk.
Re:Data fetching on the web still sucks by mewpmewp2 on Thursday January 28, 2021 @02:08AM (#25939062)
Yeah they should be capable of having a compatible type system. So if you change a field name on backend, frontend won't compile unless you fix it there too.
Re:Data fetching on the web still sucks by eyelidlessness on Wednesday January 27, 2021 @11:25PM (#25938155)
> instead of REST

Why? REST doesn't have anything to say about this. You can design your application however you prefer, and publish input and output specs however you prefer, without tailoring your application to the HTTP layer.

Re:Data fetching on the web still sucks by mewpmewp2 on Thursday January 28, 2021 @02:06AM (#25939053)
Instead of having to implement REST apis is what I mean.

It could work with REST underneath or REST being auto generated based off your function names, but you shouldn't have to deal with that. And REST in general would probably not be optimal to transfer the data here.

Re:Data fetching on the web still sucks by eyelidlessness on Thursday January 28, 2021 @11:18AM (#25944232)
I’m not sure what you think REST is suboptimal for here, the function you described maps trivially to it.

    GET /tasks?whatever=filters
I agree the part that wires up the HTTP behavior should be minimal and mostly uninvolved with your application logic. (I’m actually working on such a beast!).
Re:Data fetching on the web still sucks by mewpmewp2 on Friday January 29, 2021 @06:06AM (#25956129)
Yeah, I'm saying that you shouldn't have to think of such things like URL mapping. What if now all of sudden you want to post json as a filter? You will have to start thinking of new arbitrary ways to handle this query etc. You have to both parse this new deep nested filter you thought of in backend and also you have to stringify it in the frontend. You add so much extra overhead to try to convert the query into some form of string URI that would adhere to URL rules.

Let's say you now want to add a filter for frontend where you can choose multiple dynamic fields and their conditions can be either greater than, equal, or IN some values, I think you get the gist.

You then will have to do it something like that tasks?status=in:todo,progress

Or status=todo&status=done But then you suddenly need greaterThan query. Are you going to do tasks?priority=gt:5 for example?

Or things like that. And you have to worry if this even adheres to URL standards. Things eventually get so messy. And you are very constrained in everything you do.

And how do you typecheck all of that? Also all of this is just error prune, you have to write documentation for it and every REST api is a little bit different, people often mess up when naming the urls etc.

Re:Data fetching on the web still sucks by eyelidlessness on Saturday January 30, 2021 @11:23AM (#25970320)
> Yeah, I'm saying that you shouldn't have to think of such things like URL mapping. What if now all of sudden you want to post json as a filter? You will have to start thinking of new arbitrary ways to handle this query etc. You have to both parse this new deep nested filter you thought of in backend and also you have to stringify it in the frontend. You add so much extra overhead to try to convert the query into some form of string URI that would adhere to URL rules.

I’m not sure I even follow what you’re proposing. That URLs are insufficient for nested structural queries or that you want URL queries and POSTed JSON queries (which is not even REST) at the same time?

If it’s the former, this isn’t something a client or server should need to worry about. Server tools should make defining the API simple in the native language, and generate documentation which can provide client SDKs (again I’m building such a tool).

> Let's say you now want to add a filter for frontend where you can choose multiple dynamic fields and their conditions can be either greater than, equal, or IN some values, I think you get the gist. [...] status=in:todo,progress [...] status=todo&status=done

The most common way this is handled is either your second syntax or status[]=todo&status[]=done (or even with explicit indexes) to make clear it’s multiple values. AFAIK most major URL (de)serializers handle this automatically with no developer effort.

> priority=gt:5

Why not priority=>5? Again a library can trivially handle simple expressions like this, you don’t have to.

> And how do you typecheck all of that?

This is something the library should handle too. And it’s something I know about because I’ve built it (and again I’m working on one I can make open source). For a hint of how this might look and some prior art, check out io-ts.

> Also all of this is just error prune, you have to write documentation for it and every REST api is a little bit different, people often mess up when naming the urls etc

I bet you can already predict it, but the library should take care of this and I’m building it.

None of this should be so messy or so much work for people developing services. You got that right! But that doesn’t mean REST is bad, it means the tools for building and consuming REST services aren’t very mature. But they certainly can be.

Re:Data fetching on the web still sucks by machiaweliczny on Wednesday January 27, 2021 @02:39PM (#25932874)
Apollo client and Elixir LiveView are solving most of these.
Re:Data fetching on the web still sucks by Garlef on Wednesday January 27, 2021 @02:45PM (#25932956)
Yes. But do they automagically create and maintain database indexes for the most common requests made by your webclients?
Re:Data fetching on the web still sucks by machiaweliczny on Thursday January 28, 2021 @07:16AM (#25940899)
Should be doable. Applications only recently have a need for custom queries due to BI rising, so I guess DBs aren't yet there.

BTW IMO most of BI type stuff could be solved by just allowing direct SQL, but for this DBs would need to be written without buffer overflow bugs etc. I would love to have SQLite that has easy data-at-rest encryption amd that supports access control.

Re:Data fetching on the web still sucks by ricketycricket on Wednesday January 27, 2021 @02:51PM (#25933022)
LiveView doesn’t allow arbitrary queries (or any queries really), so this is a non-issue.
Re:Data fetching on the web still sucks by jamil7 on Thursday January 28, 2021 @02:01AM (#25939027)
Is PouchDB popular at all on the web these days? Things like Realm etc. on mobile solve this problem by just giving you a client side database to read and write from, transport to a backend is handled by a sync engine. I guess this is sort of what meteor was trying to do but it never really took off.
Re:Data fetching on the web still sucks by lxe on Wednesday January 27, 2021 @01:30PM (#25931905)
I actually think the problems outlined are pretty valid.

Yeah, there are solutions to them, but I think what the author is saying is that you have to subscribe to something batteries-included, like Meteor, or otherwise implement the solutions yourself from bits and pieces.

Re:Data fetching on the web still sucks by derefr on Wednesday January 27, 2021 @02:31PM (#25932747)
"You have to subscribe to something batteries-included, like Meteor, or otherwise implement the solutions yourself from bits and pieces."

...yes; that is, in fact, the conceptual dichotomy in solutions to essentially-complex problems. Either someone else solves them for you, or you have to solve them yourself.

I mean, yes, there is a part of the solution-space in-between these two extremes — a point where there's a batteries-included thing that someone else built but then gives away for free, perhaps as an open-source project you just have to run on your own infra. So it's mostly solved for you, and then you just do a little bit to "get the solution running" for your use-case. That point in solution-space is conspicuously absent for web data sync.

But, in domains with essential complexity, that middle-ground part of the solution-space is usually conspicuously absent. Because it takes continuous dedicated effort (i.e. labor; capital expenditure) to solve the complex problem in a cleanly-abstracted way. And it's very rare that anyone's going to go to the effort, unless they expect a return on their labor investment (by e.g. keeping the solution proprietary, and building a SaaS business around it.)

Re:Data fetching on the web still sucks by dillondoyle on Wednesday January 27, 2021 @04:21PM (#25934113)
I might intuit that 'request batching' could be benefit http/2 but probably not what they were thinking about. And you'd have to have a lot of simultaneous http requests for it to help at all.

But I'm with you on trying to batch this with your JS should not be a browser thing or even a built in standard library JS function.

One could do it themselves - maybe graphQl kind of this idea of more complex queries + maybe just throw the requests you want to make into an array and when it hits length = 5 send them all at once. But I don't see how that would help response times at all.

Re:Data fetching on the web still sucks by batoure on Wednesday January 27, 2021 @01:24PM (#25931812)
What this person said! ^

almost all of the mentioned issues have solutions out their but the author is jumping around the stack with no sense of true purpose. How does graphql impact your SPA updating views, it doesn’t..

Re:Data fetching on the web still sucks by chrisco255 on Wednesday January 27, 2021 @11:32PM (#25938209)
Of course it does, because GraphQL allows you to roll up queries for separate entities and get the data back for those in a single HTTP request. And that capability is something Relay and Apollo take advantage of. In this way a component can reference only the data it needs and the query rollup and caching happens automatically.
Re:Data fetching on the web still sucks by whateveracct on Wednesday January 27, 2021 @04:32PM (#25934253)
> Request batching: How would that happen "automatically"? By waiting to fire requests and then batching them?

Haxl is good prior art for automatic batching and concurrency. But it helps that Haskell makes the "when" very explicit thanks to its type system.

https://engineering.fb.com/2014/06/10/web/open-sourcing-haxl...

Re:Data fetching on the web still sucks by pferdone on Wednesday January 27, 2021 @01:45PM (#25932113)
Half of the stuff he mentioned are covered by apollo-graphql and libs in that eco-system: query batching, notifications (via subscriptions), automatic state updates via cache, typesafety with typescript and grapqhl-codegen. It doesn‘t seem like he even looked at the libraries he listed. :(
Re:Data fetching on the web still sucks by mewpmewp2 on Wednesday January 27, 2021 @03:31PM (#25933527)
Yeah, exactly, half of the stuff. You should be able to have all of this available by default. And it should have first class support without having to do complex setup. Also writing gql queries has always felt hacky and uncomfortable to me, without IDEs having the best support for it, and type safety? But maybe I haven't had the perfect setup.

Also graphql forces you to a certain way to resolve your data which may not always be the best fit.

Re:Data fetching on the web still sucks by bcherny on Wednesday January 27, 2021 @01:50PM (#25932178)
Sorry if it was unclear, but my argument is that while many libraries solve pieces of the problem, no one library (or standard) solves all, or even most, of these.
Re:Data fetching on the web still sucks by yrio on Wednesday January 27, 2021 @10:42PM (#25937860)
How does Ember.js compare to Angular? Both seems to aim to be a complete solution / batteries included.

(I'm not familiar with Ember.js)

Re:Data fetching on the web still sucks by karmelapple on Wednesday January 27, 2021 @11:00PM (#25937978)
We chose Ember over Angular years ago, and we’re happy with the experience. Always hope more people will try it out - I hope you do!