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?
14 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.