How multi-provider strategies work
masterA multi-provider strategy allows you to define how the SDK handles multiple providers within a single channel. You can use predefined strategies or implement a custom one.
Predefined strategies
| Strategy name | Description |
|---|---|
fallback | If the used provider returns an error, try the next in the list. |
roundrobin | Use every provider in turns. If one of them returns an error, fallback to the next. |
no-fallback | Deactivates fallback strategy. |
Custom strategies
A custom strategy is a function that takes an array of providers and returns a Sender function: (Provider[]) => Sender.
// Random strategy example
const randomStrategy = (providers) => async (request) => {
const provider = providers[Math.floor(Math.random() * providers.length)];
try {
const id = await provider.send(request)
return {id, providerId: provider.id}
} catch (error) {
error.providerId = provider.id
throw error
}
}
new NotifmeSdk({
channels: {
email: {
providers: [...],
multiProviderStrategy: randomStrategy
}
}
})