[Supabase] Realtime Implementation Proposal

Hello, this will mostly be directed to the WeWeb devs behind the Supabase Realtime, a.k.a @Alexis.

I have snooped around a little bit and the implementation of the sync event at least, within the Presence is really not the most optimal, since it doesn’t inform the users about the state of the current session (that’s what it’s supposed to do), now basically the sync is not so useful with the current implementation, it just logs ‘sync’.

The code responsible for this is the following:

 if (presence) {
            _channel.on(
                'presence',
                {
                    event: '*',
                },
                e => {
                    wwLib.wwWorkflow.executeTrigger(this.id + '-realtime:presence', {
                        event: { channel, data: e },
                        conditions: { channel, event: e.event },
                    });
                }
            );
        }
        _channel.subscribe();
}

Currently in every e (event) that is being passed into the callback when the presence fires, has the joined / left presences, which is as should be, but the actual presences are an empty array - always.

What I’d propose is something such as the following, as also the Supabase documentation suggests (Listen to presence sync) - pseudo code without the wwLib.executeTrigger():

e => {
   // Enrich the event with the current presences from the _channel
   const event = {...e, currentPresences: _channel.presenceState()}
   // Trigger the event ...
}

Or if you wanted to mantain the integrity, it could be something like the following:

e => {
    // Enrich the event with the current presences from the _channel
    const state = _channel.presenceState(); // Get the state

    // Map the state object's keys into an array so that it mantains the array convention previously used
    const currentPresences = Object.keys(state)
      .map((key) => {
        return {
          id: key,
          data: state[key]
        }
      });

    const event = { ...e, currentPresences };
   // Trigger the event ...
  }

One could extend this same logic to all of the events, since all of the events have the currentPresences as an empty array, no matter what, since Supabase apparently doesn’t handle it as one would expect.

I’ll be grateful for any feedback, I feel like this could improve the usability of the current Supabase’s functionality. Right now in order to achieve hassle-free results I yet again have to resort to code.

Thanks!

2 Likes

^^ What he said :smile:

I’m looking at using this, for this exact use case. But as you say here I’m having issues with seeing the presence array as well.

Did you get anything more from this, would be great if this worked so we could do something to deal with realtime timeouts.

Hi, I ended up implementing it with the custom code. Never got an official answer from WeWeb cuz, why would they bother.

Yea that figures, so you just use the wwLib supabase plugins to work with in javascript custom code? I’m still tryna find where I can access those actual arrays and some other presence commands I’ve seen around but don’t seem to have access to in weweb. Or are you keeping track a custom way?

I’ve used the wwLib

1 Like