Pure elm Firebase client

562 views
Skip to first unread message

Atle Wee Førre

unread,
Aug 26, 2017, 11:58:31 AM8/26/17
to elm-dev
Hi, (My first ever message here)
I'm quite new to programming, and it is only a hobby for now.
I have used firebase through ports for a while and it works fine, but I would love to use firebase in a more functional / elm way + avoid writing any javascript.
I have made a simple native module that works fine, but this is suboptimal since native/kernal code should be avoided as much as possible and the javascript firebase client is very object oriented.

So this week I started builing a pure elm implementation of the Google firebase client, without using native/kernal code.
Trying to start with the minimal implementation just to get something working and then build on from there.
The effectmodule uses elm Websocket.LowLevel to communicate with firebase.

Status now:
Opening and closing of the firebase websocket connection is done automatically based on whether there is active subscriptions in the Main program or not.
I can subscribe to multiple firebase database locations/paths and the effectmodule will send the updated data to the correct subscription in my elm program.
I can also write firebase data by sending commands to the effectmodule. Large multi websocket frame messages is also working.

My problem:
Subscription is cancelled if nothing is sent for about a minute.
To keep the firebase subscription live a value has to be sent every 45 second. So if there is no activity I need to send a websocket frame with "0" if it has been more that 45seconds since last firebase data frame. (The same way as the javascript client works)
I'm having problems solving this.   Does anyone have an idea how to solve this? I guess if there is no activity in the elm program It will not trigger "onEffects" and "onSelfMessage" in the effectmodule?

It can be handled in the main elm program, but I feel lowlevel stuff like websocket keepalive belongs in the firebase package/effectmodule. ?
Is it possible to use subscriptions inside an effect-module in some way? (subscribe to the Time package)
Is it possible to spawn a separate process that sleeps for 45sec and then change the sleep time everytime a websocket frame is sent? :)

Atle Wee Førre

unread,
Sep 7, 2017, 9:20:08 AM9/7/17
to elm-dev
I think i found a way to make it work.
Upon connection I Spawn a process that sleeps for 45s and then sends a keepalive message. The PID of this process is kept in state.
Everytime a frame is sent or reiceived this process is killed and a new one for 45s is started again.
I guess it works, but does anyone have a better suggestion?

Atle Wee Førre

unread,
Sep 7, 2017, 9:20:08 AM9/7/17
to elm-dev
This solution solved my issue:
Every time I receive a message a KeepAlive is sentToself:

case selfMsg of
   
ResetKeepAlive ->
      let    
        sendKeepAliveMessage
=
          WS
.send socket "0"
           
&> Platform.sendToSelf router ResetKeepAlive
     
in
         
Process.kill currentKeepAlivePid
           
&> Process.spawn (Process.sleep 45000 &> sendKeepAliveMessage)
   
....



lørdag 26. august 2017 17.58.31 UTC+2 skrev Atle Wee Førre følgende:
Reply all
Reply to author
Forward
0 new messages