digitallyinduced / ihp

🔥 The fastest way to build type safe web apps. IHP is a new batteries-included web framework optimized for longterm productivity and programmer happiness
https://ihp.digitallyinduced.com/
MIT License
4.91k stars 194 forks source link

How to cross JS language barrier #128

Open rowanG077 opened 4 years ago

rowanG077 commented 4 years ago

I can't find this in the documentation. But I would like to call JS function (normal and async functions) from within ihp. Mainly to leverage the huge number of existing JavaScript libraries and also to call into the browser API's like file reading, local storage etc.

If this is possible could you provide an example or link to documentation on how I can achieve this?

cies commented 4 years ago

Seems like you want to call from server side code to code running in the browser. You will likely need WebSockets for this. I'm not much familiar with IHP, but I saw some WS lid is actually a dependency, yet I think that may be there for the hot code reloading and is not yet exposed by the APIs for devs. IHP is, from what I read in the docs, an old skool request-response based framework: going with WebSockets requires quite a bit different architecture on the server side.

mpscholten commented 4 years ago

Hey together :) Right now WebSockets aren't supported in IHP userland (it's planned).

@rowanG077 if you have a more specific example on what you want to do, maybe I can guide you in the right direction of how to do it the IHP-way.

revskill10 commented 4 years ago

How about using GHCJS with nix ?

mpscholten commented 4 years ago

IMO GHCJS is a bit too heavyweight for most use cases. We can solve a lot of use cases by offering something like Phoenix LiveViews. When an SPA is really needed, I'd take a look at using Elm. We could add an Recipe to the documentation on using Elm with IHP.

revskill10 commented 4 years ago

@mpscholten It's awesome. I'd love to see an example with API in IHP and ELM as the frontend.

rowanG077 commented 4 years ago

Thanks for the quick responses. I have some peripheral hardware that can talk via serial. I can leverage this in a "standard" browser setting by using web serial to connect to it. In addition my goal was to plot data that comes from this serial port in real-time. I don't particularly want to implement a plotting library myself so instead I would want to use a mature JavaScript plotting library and just push my data to it via a JS FFI or something.

But from the discussion I understand that ihp is server-side only and it doesn't actually run code on the client side.

mpscholten commented 4 years ago

But from the discussion I understand that ihp is server-side only and it doesn't actually run code on the client side.

Maybe you could pass the serial data to the view via json and then plot it by including the script via the usual <script> tag way?

tluyben commented 4 years ago

IMO GHCJS is a bit too heavyweight for most use cases. We can solve a lot of use cases by offering something like Phoenix LiveViews. When an SPA is really needed, I'd take a look at using Elm. We could add an Recipe to the documentation on using Elm with IHP.

I think LiveViews fixes most of these cases.

I think GHCJS should be given a chance though; I would like to not use JS/Elm or anything else for cases where Liveviews do not work. But maybe it's a dream. Next of Elm I prefer Purescript if not Haskell.