packages/react/README.md
Novu provides the @novu/react a React library that helps to add a fully functioning Inbox to your web application in minutes. Let's do a quick recap on how you can easily use it in your application.
Refer to the Novu documentation for the complete React quickstart guide.
@novu/react npm package in your react appnpm install @novu/react
The keyless mode is designed for local testing and experimentation, letting you use Novu's Inbox component without any configuration. Just import and render.
import React from 'react';
import { Inbox } from '@novu/react';
export function App() {
return <Inbox />;
}
To connect the Inbox component with your Novu environment and real subscribers, set the applicationIdentifier and subscriberId
import { Inbox } from '@novu/react';
function Novu() {
return (
<Inbox
subscriber='SUBSCRIBER_ID'
applicationIdentifier='APPLICATION_IDENTIFIER'
/>
);
}
By default, Novu's hosted services for API and socket are used. If you want, you can override them and configure your own.
import { Inbox } from '@novu/react';
function Novu() {
return (
<Inbox
backendUrl='YOUR_BACKEND_URL'
socketUrl='YOUR_SOCKET_URL'
subscriber='SUBSCRIBER_ID'
applicationIdentifier='APPLICATION_IDENTIFIER'
/>
);
}
You can use the open prop to manage the Inbox popover open state.
import { Inbox } from '@novu/react';
function Novu() {
const [open, setOpen] = useState(false);
return (
<div>
<Inbox
subscriber='SUBSCRIBER_ID'
applicationIdentifier='APPLICATION_IDENTIFIER'
open={isOpen}
/>
<button onClick={() => setOpen(true)}>Open Inbox</button>
<button onClick={() => setOpen(false)}>Close Inbox</button>
</div>
);
}
You can pass the localization prop to the Inbox component to change the language of the Inbox.
import { Inbox } from '@novu/react';
function Novu() {
return (
<Inbox
subscriber='SUBSCRIBER_ID'
applicationIdentifier='APPLICATION_IDENTIFIER'
localization={{
'inbox.status.archived': 'Archived',
'inbox.status.unread': 'Unread',
'inbox.status.options.archived': 'Archived',
'inbox.status.options.unread': 'Unread',
'inbox.status.options.unreadRead': 'Unread/Read',
'inbox.status.unreadRead': 'Unread/Read',
'inbox.title': 'Inbox',
'notifications.emptyNotice': 'No notifications',
locale: 'en-US',
}}
/>
);
}
When Novu's user adds the Inbox component to their application, developers need to provide a subscriber prop with the value of their customer's subscriberId, along with an application identifier that serves as a public key for API communication.
A malicious actor can access the user feed by accessing the API and passing another subscriberId using the public application identifier.
HMAC encryption will make sure that a subscriberId is encrypted using the secret API key, and those will prevent malicious actors from impersonating users.
In order to enable Hash-Based Message Authentication Codes, you need to visit the admin panel In-App settings page and enable HMAC encryption for your environment.
<!-- <Frame caption="How to enable HMAC encryption for In-App Inbox"> </Frame> -->import { createHmac } from 'crypto';
const subscriberHash = createHmac('sha256', process.env.NOVU_API_KEY).update(subscriberId).digest('hex');
<Inbox
subscriber={'SUBSCRIBER_ID_PLAIN_VALUE'}
subscriberHash={'SUBSCRIBER_ID_HASH_VALUE'}
applicationIdentifier={'APPLICATION_IDENTIFIER'}
/>
Note: If HMAC encryption is active in In-App provider settings and
subscriberHashalong withsubscriberIdis not provided, then Inbox will not load
If you're using the context prop to pass additional data (e.g., tenant information, environment, etc.), you should also generate a contextHash to prevent context tampering:
import { createHmac } from 'crypto';
import { canonicalize } from '@tufjs/canonical-json';
const context = { tenant: 'acme', app: 'dashboard' };
const contextHash = createHmac('sha256', process.env.NOVU_API_KEY)
.update(canonicalize(context))
.digest('hex');
<Inbox
subscriber={'SUBSCRIBER_ID_PLAIN_VALUE'}
subscriberHash={'SUBSCRIBER_ID_HASH_VALUE'}
context={{ tenant: 'acme', app: 'dashboard' }}
contextHash={'CONTEXT_HASH_VALUE'}
applicationIdentifier={'APPLICATION_IDENTIFIER'}
/>
Note: When HMAC encryption is enabled and
contextis provided, thecontextHashis required. The hash is order-independent, so{a:1, b:2}produces the same hash as{b:2, a:1}.