We would want to send timely emails to subscribers about updates/changes to the database.
What would be an optimum approach to this.
Is considering SignalR a good path?
Example:
Let us say we have a list of phones with specific features in the database. User wants to get notified whenever a new phone with front camera of 8 Mega pixels comes into market.
Considered storing email subscribers, and their conditions, check database every night, and send emails.
Read about SignalR and real time updates but could not wrap my head over its applications. Can it be applied to scenario that I have mentioned?
1
Sending emails to subscribers every once in a while isn’t really a real-time requirement. Subscribers would read your emails after a few minutes at the very least, so why bother sending them within microseconds of updates.
It would be best suited for a scheduled task that runs in set intervals like 60 seconds.
Here’s a simple task scheduling technique that StackOverflow itself used in its early days:
- At startup, add an item to the HttpRuntime.Cache with a fixed expiration.
- When cache item expires, do your work (send your emails)
- Re-add the item to the cache with a fixed expiration.
This goes into Global.asax:
private static CacheItemRemovedCallback OnCacheRemove = null;
protected void Application_Start(object sender, EventArgs e)
{
AddTask("DoStuff", 60);
}
private void AddTask(string name, int seconds)
{
OnCacheRemove = new CacheItemRemovedCallback(CacheItemRemoved);
HttpRuntime.Cache.Insert(name, seconds, null,
DateTime.Now.AddSeconds(seconds), Cache.NoSlidingExpiration,
CacheItemPriority.NotRemovable, OnCacheRemove);
}
public void CacheItemRemoved(string k, object v, CacheItemRemovedReason r)
{
// do stuff here if it matches our taskname, like WebRequest
// re-add our task so it recurs
AddTask(k, Convert.ToInt32(v));
}
Source: http://blog.stackoverflow.com/2008/07/easy-background-tasks-in-aspnet/