Headless using Novu/JS
I am using the following snippet on our react js app
const novu = useMemo(() => {
return new Novu({
applicationIdentifier: applicationId,
subscriberId: memberId,
});
}, [applicationId, memberId]);
novu.on('notifications.notification_received', data => {
console.log('new notification =>', data);
getNotifications(1);
});
I see websockets being called in the inspector but the event does NOT get fired. Am I missing anything?
20 Replies
When I call list, it's always empty
const getNovuNotifications = useCallback(async () => {
const {data} = await novu.notifications.list({limit: 30});
setList(data?.notifications);
console.log('NOVU-list', data?.notifications);
}, [novu]);
I know the subsciber has existing notifications when I call the novu api.
https://api.novu.co/v1/messages?subscriberId=759291&page=0&limit=15
This is the default websocket url: wss://ws.novu.co/socket.io
@Mark Garcia
Could you please share complete code?
parts redacted but this is how it's implemented in a Provider component
export const GlobalNoticeProvider = ({
children,
}: GlobalNoticeProviderProps): JSX.Element => {
const applicationId = process.env.NEXT_PUBLIC_NOVU_APPLICATION_ID as string;
const memberId = getUserIdFromStoredToken() as string;
const novu = useMemo(() => {
return new Novu({
applicationIdentifier: applicationId,
subscriberId: memberId,
});
}, [applicationId, memberId]);
novu.on('notifications.notification_received', data => {
console.log('new notification =>', data);
getNotifications(1);
});
const getNovuNotifications = useCallback(async () => {
const {data} = await novu.notifications.list({limit: 30});
setList(data?.notifications);
console.log('NOVU-list', data?.notifications);
}, [novu]);
return ( <GlobalNoticeContext.Provider value={value}> <> {openNotice && ( <NoticeDialog notice={openNotice} onClose={handleOnClose} /> )} {children} </> </GlobalNoticeContext.Provider> ); }
return ( <GlobalNoticeContext.Provider value={value}> <> {openNotice && ( <NoticeDialog notice={openNotice} onClose={handleOnClose} /> )} {children} </> </GlobalNoticeContext.Provider> ); }
Thanks. I will take a look
Hi @Pawan Jain just following up on this.
@Mark Garcia
Checkout this code 👇🏻
Thanks @Pawan Jain but it still does NOT trigger the new_notification event. I've created custom hook for this implementation
import {useEffect, useMemo} from 'react';
import {useNotification} from '@hooks/useNotification';
import {Novu} from '@novu/js';
import {useAuthentication} from './useAuthentication';
export const useNovuEvents = () => {
const {getUserIdFromStoredToken} = useAuthentication();
const {getNotifications} = useNotification();
const applicationId = process.env.NEXT_PUBLIC_NOVU_APPLICATION_ID as string;
const memberId = getUserIdFromStoredToken() as string;
const novu = useMemo(() => {
return new Novu({
applicationIdentifier: applicationId,
subscriberId: memberId,
});
}, [applicationId, memberId]);
useEffect(() => {
// Set up event listener
novu.on('notifications.notification_received', data => {
console.log('new notification =>', data);
getNotifications(1);
});
}, [novu, getNotifications]);
};
Then calling this in our GlobalNotice provider
useNovuEvents();
I can confirm websocket is established and the useEffect hook gets called.

This is the generated websocket domain: wss://ws.novu.co/socket.io/?token=
@Mark Garcia, you just advanced to level 2!
Above is the sample curl I send out to trigger new notification but 'notifications.notification_received' event is not picking it up and it does NOT fire.
I confirm that I receive the notification.

@Mark Garcia
Did not get chance to check with wrapping this in hooks
What is your usecase with wrapping it over a hook?
We want to eventually create a component that allows for multiple notification provider so we would like to place each provider in its own custom hook. But I also did the SUGGESTED implementation and I got the same result.
Do you have any reports of anything blocking your websocket requests or interferring with it? I'll try to create a new project and try this there.
I've created a blank nextjs project and even that didn't work
'use client'
import Image from "next/image";
import { Novu } from "@novu/js";
import { useEffect, useMemo } from "react";
export default function Home() {
const novu = useMemo(() => {
return new Novu({
applicationIdentifier: '6nupmbyERXRf',
subscriberId: '759291',
})
}, []);
useEffect(() => {
novu.on("notifications.notification_received", (data) => {
console.log("new notification =>", data);
});
novu.on("notifications.unread_count_changed", (data) => { console.log("new unread notifications count =>", data); }); }, [novu]) return ( <div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20 font-[family-name:var(--font-geist-sans)]"> EMPTY HTML </div> ); }
novu.on("notifications.unread_count_changed", (data) => { console.log("new unread notifications count =>", data); }); }, [novu]) return ( <div className="grid grid-rows-[20px_1fr_20px] items-center justify-items-center min-h-screen p-8 pb-20 gap-16 sm:p-20 font-[family-name:var(--font-geist-sans)]"> EMPTY HTML </div> ); }
@Mark Garcia
Are you self hosting Novu because I can not reproduce this on novu cloud
No, we're using novu cloud services again.
This is the only thing missing so that we can launch the notification feature
This is when I try to get notification list
[GET] https://api.novu.co/v1/inbox/notifications?limit=10
Generated Bearer: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJfaWQiOiI2NmY5ZmY3ZGVjMmM5MTBiMmVmYjBjMDQiLCJlbWFpbCI6ImRldmVsb3BlckBob21ldGVzdGVyY2x1Yi5jb20iLCJvcmdhbml6YXRpb25JZCI6IjY2YTJkM2E4MzIzYmEzOWZhY2VjZTBjNCIsImVudmlyb25tZW50SWQiOiI2NmEyZDNhODMyM2JhMzlmYWNlY2UwZDIiLCJzdWJzY3JpYmVySWQiOiI3NTkyOTEiLCJpYXQiOjE3NDc2MTQ1MjcsImV4cCI6MTc0ODkxMDUyNywiYXVkIjoid2lkZ2V0X3VzZXIiLCJpc3MiOiJub3Z1X2FwaSJ9.tYVfiLl72UEPUi6ZfEtNIor6AIsFhl5Pwt35JdXm09A
I am getting empty data here.
const [notification, setNotification] = useState<Notification[] | null>([]);
const getNotices = useCallback(async () => {
const { data: notifications, error } = await novu.notifications.list();
if (!error) {
setNotification(notifications?.notifications ?? []);
}
}, [novu]);
Do I need to whitelabel domains? Currently testing this in my local machine.
Maybe any CORS issue if I test in local machine? Where can I white list origin domains?
Hi @Pawan Jain we need to resolve this with urgency
@Mark Garcia, you just advanced to level 3!
@Pawan Jain please let me know what are the next steps
@Mark Garcia
Could you please send us an email to support@novu.co from the same account you signed up on Novu
Please share your application identifier. I will check in logs if there is some issue
Ok, will also include the blank next js app with the basic implementation.