I’m using wix-realtime to send messages to the frontend to notify the page to request updated data.
I have it working well in preview mode but the live page does not receive the messages.
I’m able to subscribe successfully. I have the default permissions set to “read: true”. The problem is when a publish() is called the message is never received.
The publish() is called from a backend file that uses wix-data to update a collection then calls a function to publish a notification to the frontend.
I’ve checked to make sure the name of the channel matches on both the publish() and subscribe() methods.
Here’s the code I’m using in the realtime-permissions.js file:
export function realtime_check_permission(channel, subscriber) {
// set default permissions
let permissions = { read: true };
console.info(`Checking for ${channel.name} channel in realtime `, subscriber);
console.info(`returning permissions `, permissions);
return permissions;
}
The above console.info() logs appear in the wix logs and indicate the “read: true” permissions are being passed when subscribe() is called.
The code you’ve provided is what runs the checks for the permission, and does not set the permission; that’s not going to help us in order to pinpoint the issue you’re facing If the check is returning true and running well in Preview, then its most likely that you haven’t ‘set’ the permission right.
I’m linking a forum post below where another user wanted to achieve something very similar to what you are trying to do, so I’ll refer you to my solution, which works as expected; and I’ll recommend you go through the entire thread from the beginning and if needed, try building out this functionality on a new test site first, and once you get that right - implement it in your current site.
I’m using exactly the same approach you used in your post except I’m not calling the publish from a data hook. It’s being called when a http-functions call happens.
So here’s my realtime-permissions.js file. I changed it to use the permissionsRouter functions.
I’m still not having success. I’m also noticing the console.info() logs used in this file are not coming through on the Wix Logs. Is it even running these? I’ve double checked the file name spelling and it’s correct. It’s located in my backend files. Maybe I should recreate the file.
The problem is this: The publish() will not work when using it in the http-functions.js file. I had to move the publish() call to the data hook (data.js) for onInsert and onUpdate for the collection I’m using.
I originally had the publish() call in a js file that was imported by the http-functions file. So I tried moving the call to the http-functions file but it still wouldn’t work.
It must have something to do with permissions from within the http-functions. I even tried using the elevate() function but still no success. It’s funny that it works fine from the preview mode.
I think I’ll submit a bug report on this. I hope this helps someone else out there that goes down this rabbit hole.
One of my sites does exactly this: publishing the message to a channel when an http-function is fetched. And exactly like I have explained it in the post I referred to, I have the publish function set up in a .jsw file in the backend which I then call in the http-function similar to how I’ve called it in the data hooks, right before returning the response body.
This approach works flawlessly - but I’ll point out that my code uses a .jsw file and not a .web.js file - which is what is now the recommended format instead of .jsw
So given the nature of your problem, it might be that you are using a web.js file with the publish function in it, the permissions of which might not be configured correctly which is what might be causing this issue.
I would suggest that you try using the .jsw format. Although deprecated, it will continue to work, so that shouldn’t be a problem for the foreseeable future - as long as it fulfils the purpose.
I have the publish function set up in a .jsw file in the backend which I then call in the http-function similar to how I’ve called it in the data hooks, right before returning the response body.
I’ll keep that in my back pocket for the future. I’m new to Wix so I’ve never used the jsw files. Maybe they have different persmissions than a regular js file. When you think about it, calling publish() from the data hooks is the best option in this case because it will catch any updates or inserts no matter from where it occurs.
So given the nature of your problem, it might be that you are using a web.js file with the publish function in it, the permissions of which might not be configured correctly which is what might be causing this issue.
Actually it was in a regular js file that was imported by the http-functions file. Most of the examples I’ve seen use a web.js file so it would probably work fine in that case.
I’m just glad it’s finally working. Banging your head against the wall is no fun. Thanks for your help!