Fork me on GitHub
#hyperfiddle
<
2023-06-24
>
bahulneel13:06:49

Has anyone had experience running electric as a service worker, I have an existing UI built in Nuxt that I want to integrate with an electric+xtdb backend? Is this event the correct approach, I’d appreciate any comments/suggestions.

Dustin Getz13:06:47

what is a service worker

bahulneel13:06:51

It allows you to run JS (with limitations i.e. no DOM) in a separate thread in the browser.

bahulneel13:06:14

You communicate with the page via message passing

bahulneel13:06:57

sorry, I mean web workers, service workers are similar but network traffic specific

Dustin Getz14:06:23

by "run electric as a web worker" you mean you want to run electric entirely in a thread, so as to be isolated from Nuxt, keeping the Nuxt UI and yet talking to an Electric backend, so as to load data from electric into a clientside datastore for your Nuxt components? What exactly do you want to do?

Dustin Getz14:06:43

I would ask you to show me some application code sketches of what your code looks like today (highlighting the problem you want to solve) and also what you would like your code to look like

bahulneel14:06:48

After reading your documentation I'm not exactly sure what I want to do. I'll go and think about it and come back with something concrete.

šŸ‘ 2
Dustin Getz15:06:18

fwiw we are just now starting a Fulcro integration, which is of the form of embedding an Electric component inside the Fulcro/React component tree, which can accept props from Fulcro and interact with any services in scope (like routing). In this worldview the electric subtree performs its own IO where desired while also able to receive data from Fulcro

šŸ‘€ 4
bahulneel15:06:59

Interesting, I think I need something similar. Is there a good place to look at integration patterns/options

Dustin Getz15:06:20

this slack channel indexes everything interesting, i dont recall anyone having worked this out deeply yet

bahulneel15:06:24

Ok thanks, I think I need to grok electric a bit more, the answer may be out there already. I'll probably implement the basis of what I want to achieve, starting with the xtdb starter app, without the integration and then, at least, I'll know what I need to solve.

Sagar Vrajalal19:06:15

Maybe a very silly question but how do I go about changing the default page title? I copied the server code from electric-starter-app

<head>
  ...
  <title>Hyperfiddle</title>
  ...
</head>

Dustin Getz19:06:47

edit index.html

Dustin Getz19:06:20

if you want to set it dynamically, you'll need a side effect in your router code

Sagar Vrajalal20:06:55

I actually wasn't using index.html . That fixed it. I would like to set it dynamically though, could you explain what you mean by a side effect in your router code? I don't quite understand how it gets generated when it's missing.

Dustin Getz20:06:31

index.html is not generated, it's a static resource in the starter app, Electric is solely a websocket abstraction

šŸ’” 1
Dustin Getz20:06:04

It's up to you how to generate index.html, the only requirement is that you integrate our websocket middleware

Dustin Getz20:06:22

https://biffweb.com/p/how-to-use-electric/ may help if you'd like a more framework-y approach

Dustin Getz20:06:31

(set! (,-innerText (js/document.querySelector "title")) "hello world") (pseudocode) is what i meant by side effect

ā¤ļø 2
Sagar Vrajalal20:06:11

Thanks. I was already doing JS interop to include stylesheets! I guess this was more of a CLJS/JS DOM manipulation question more than anything else.

Dustin Getz20:06:45

oh, you have your own backend already it seems

Dustin Getz20:06:13

Sorry, i dont quite understand your setup but glad you're sorted

Sagar Vrajalal20:06:19

Basically instead of using index.html I did something of the sort :

(defn add-tag
  [{:keys [tag id props]}]
  (when-not (.getElementById js/document id)
    (let [element (.createElement js/document tag)]
      (doseq [[attr-key attr-val] (merge props {"id" id})]
        (.setAttribute element attr-key attr-val))
      (.appendChild (.-head js/document) element))))

Sagar Vrajalal20:06:19

The rest is a modified version of xtdb-starter-app

Dustin Getz20:06:19

what serves the script tag?

Sagar Vrajalal20:06:03

(def electric-main
  (hyperfiddle.electric/boot ; Electric macroexpansion - Clojure to signals compiler
   (binding [hyperfiddle.electric-dom2/node js/document.body]
     (set! (.-textContent (js/document.querySelector "title")) "Hello World")
     (mapv add-tag meta-tags)
     (Main.))))

Sagar Vrajalal20:06:57

I'm quite new to frontend, JS and CLJS in general, so I'm just experimenting

Sagar Vrajalal20:06:18

sorry, not sure what you mean?

Dustin Getz20:06:43

someone has to serve the frontend js entrypoint and inject it into the browser so it runs

Dustin Getz20:06:56

<script src="app.js">

Sagar Vrajalal20:06:00

As I said I'm just using the server code from electric-starter-app so I'm not sure how that's working. https://github.com/hyperfiddle/electric-starter-app/blob/main/src/electric_server_java11_jetty10.clj I can see the script tag in the body

<script type="text/javascript" src="/js/main.js"></script>

Dustin Getz20:06:27

ah ok, that starter server does indeed serve index.html from the resources

Sagar Vrajalal20:06:37

I think shadow-cljs is doing it?

((requiring-resolve
  'shadow.cljs.devtools.server/start!))
((requiring-resolve
  'shadow.cljs.devtools.api/watch) :dev)
;; Start electric compiler and server
(ring/run-jetty
 (http-middleware "public" "public/js/manifest.edn")
 opts)

Sagar Vrajalal20:06:30

Because I'm not using index.html

s-ol14:06:32

@U01B1CZQ9PF: the electric starter app contains an index.html in resources/public: https://github.com/hyperfiddle/electric-starter-app/blob/main/resources/public/index.html There always has to be a HTML page, it's not possible to have a website without it. It could be generated "on the fly" instead of being loaded from disk, but the browser needs HTML to load a website, since it is only the HTML that can tell the browser what JS code to load.

ā¤ļø 2
Sagar Vrajalal15:06:20

Thank you, thats helpful.