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!