How to create notifications on a adonis edge website

I have a website written using adonisjs and front end uses edge templates, when user signs in all notification should show, how can it be accomplished.

Take a look at web sockets. Adonis provides web sockets provider. see here.

Hi @zameer21 and welcome to community!

I think I already answered someone similar question in Discord…

What kind of notifications you want? In page notifications under bell icon or similar?
Or some sort of toast notifications or email notifications or push notifications?

Can you elaborate little bit more what kind of notifications, since there are many of those :slight_smile:

Yes In page notification under bell icon, yes something similar to that.

Thanks for your input, i already went through web sockets, one of the issue i found is how do you emit from HTTP controllers, i get error when i go through the document example that the value of the channel and topic is null, despite me specifying it on socket.js.


First of all thanks for reaching out, adding to already what i mentioned, i never worked on notification before, need understand the architecture, a website might have multiple users, and all get notification, some might dismiss them or mark them read, how do i capture that for each user, and when a new notification comes we want the flag to blink. I hope that is enough information.

Hi again!

There in general 2 ways how to solve it. One is to make websocket connection and other is to poll backend.

You can make frontend make ajax query to backend every n secounds to check if there are any new notificiations to see. It can be done just over regular REST endpoints.

For WS it’s slightly more complicated, but also more scalable.

For your question: How to emit WS event from HTTP controller, here’s code example:

And one thing I usually do when I can’t figure out with adonis specific is
doing it in traditional node.js way. (Like using MongoDb with mongoose).

see how real time things are achieved with expressjs. You can go that way.

Not recommended but it will be a quick fix to get your app up and running and you can figure out how to do it in adonis way with time to breathe.

thanks for the input for notifications, i will try the poll backend approach, if you have multiple users we will have to capture the flag of reading of the message for each user.
As for the webservice call from HTTP controller, i tried that using this code, i dont have a topic.

const subscriptions = Ws.getChannel('chat')

if (subscriptions) {
But it returns null.

My adonis version is 4.1, so does that mean i cant emit from HTTP controller.

I understand that in order to use the broadcast you must do it from the WS controller

> adonis make:controller RequestController --type ws


const requestChannel = Ws.getChannel('requestChannel:*');
let topic = restaurant.topic(`requestChannel:requests_xx`);
topic.broadcast('message', 'Hello');