4 ms·
What if you could call your server functions directly from your client?
- GinQuin 4y agoI just released Gravity, an open-source JS/TS framework that let you call functions defined server-side directly in your client. Gravity is a new way to communicate between server and client. No more query language to learn. No more unsafe REST apis. Just 100% end-to-end typesafe good old functions. It can integrate with modern front-end frameworks like React, Svelte, Solid and Vue. I’m already using it on the SAAS I’m working on full-time, hope you will like it as much as I do
- wruza 4y agoAmazing how people can live without it and never complain. The first express routes I define in new (home) projects are /bus/{call,notify}, which do the same under the similar js api (no type checking, no proxying). Maybe I should ditch it in favor of this at some point. My api looks something like this, minus sugar and `this` bindings: bus.method(“auth.login, (params, client) => {…}) bus.on(“auth.logged_in”, (params, client) => {…}) bus.call(“auth.login”, …params) bus.notify(“auth.logged_in”, …params) You can also call/notify in every direction (via `client` handle to one, via `bus` handle to all clients). Works through web sockets. If you don’t mind, I’d suggest considering notification mode, which doesn’t require awaits, and uses navigator.sendBeacon() under the hood. For events that do not assume any answer by design, like “user moved to section X”. Also, does it by chance have call/result logging?
- GinQuin 4y agoThe `navigator.sendBeacon()` is an awesome idea, I will definitly implement it in the future (it’s not so hard to do actually). I would also like to think about how to have the same « safe » communication system for sockets. It’s more complicated. What do you mean by call/result logging?
- wruza 4y agoWhat do you mean by call/result logging? Just seeing calls with arguments and return values in some sort of log[file]. I like to see the traffic between ends while debugging. Libraries usually provide a logger callback in initial options, like this: new Foo({ …, log: (level, …values) => {…} }) I think the last time I browsed Gravity manual, it had some sort of “beforeCall” event where it’s possible to log without a special callback.
- GinQuin 4y agoI created an issue here for the sendBeacon feature if you want to share your ideas: https://github.com/digital-loukoum/gravity/issues/1 https://github.com/digital-loukoum/gravity/issues/1
- carpenecopinum 4y agoI've been using Next.js (with typescript) a lot and API routes always felt slightly wrong to me, since matching the types involved in the requests between client and server is always mostly manual and thus error-prone. I found myself thinking "Too bad Next.js does API handling itself, so I won't be able to use this tool easily", but then I saw that they already took care of that: https://gravity.digitak.dev/docs/frameworks/next https://gravity.digitak.dev/docs/frameworks/next . So I might actually end up using this. Thanks for sharing it here!
- GinQuin 4y agoYour welcome, I hope you will enjoy it! I just released a fix for React/Next stores, so make sure you are using the latest Gravity version ;)