<div dir="ltr"><p style="clear:both;vertical-align:baseline;line-height:17.600000381469727px;font-size:13.600000381469727px;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 1em;border:0px;padding:0px">
HI guys,</p><p style="clear:both;vertical-align:baseline;line-height:17.600000381469727px;font-size:13.600000381469727px;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 1em;border:0px;padding:0px">
This is my first post to the group. You will be seeing a lot more of me from now on as I have started using erlang actively :) Here is the question</p><p style="clear:both;vertical-align:baseline;line-height:17.600000381469727px;font-size:13.600000381469727px;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 1em;border:0px;padding:0px">
<br></p><p style="clear:both;vertical-align:baseline;line-height:17.600000381469727px;font-size:13.600000381469727px;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 1em;border:0px;padding:0px">
Consider a very common scenario</p><p style="clear:both;vertical-align:baseline;line-height:17.600000381469727px;font-size:13.600000381469727px;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 1em;border:0px;padding:0px">
1.A user comes online/or goes offline (i.e his status changes)</p><p style="clear:both;vertical-align:baseline;line-height:17.600000381469727px;font-size:13.600000381469727px;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 1em;border:0px;padding:0px">
2.His friends are notified of the status change.</p><p style="clear:both;vertical-align:baseline;line-height:17.600000381469727px;font-size:13.600000381469727px;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 1em;border:0px;padding:0px">
I am trying to solve the notification problem in erlang using <a href="https://github.com/uwiger/gproc" rel="nofollow" target="_blank" style="color:rgb(74,107,130);margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;text-decoration:none">gproc</a></p>
<p style="clear:both;vertical-align:baseline;line-height:17.600000381469727px;font-size:13.600000381469727px;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 1em;border:0px;padding:0px">
There are two solutions which immediately come to mind</p><h3 style="vertical-align:baseline;line-height:1.3;font-size:15.199999809265137px;font-family:'Trebuchet MS','Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 1em;word-wrap:break-word;border:0px;padding:0px">
Solution 1</h3><p style="clear:both;vertical-align:baseline;line-height:17.600000381469727px;font-size:13.600000381469727px;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 1em;border:0px;padding:0px">
1.Create a channel for the user</p><p style="clear:both;vertical-align:baseline;line-height:17.600000381469727px;font-size:13.600000381469727px;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 1em;border:0px;padding:0px">
2.Subscribe his friends to his channel(iterative process).</p><p style="clear:both;vertical-align:baseline;line-height:17.600000381469727px;font-size:13.600000381469727px;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 1em;border:0px;padding:0px">
3.Notify them on state change.</p><h3 style="vertical-align:baseline;line-height:1.3;font-size:15.199999809265137px;font-family:'Trebuchet MS','Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 1em;word-wrap:break-word;border:0px;padding:0px">
Solution 2</h3><ol style="list-style-position:initial;vertical-align:baseline;line-height:17.600000381469727px;font-size:13.600000381469727px;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 1em 30px;border:0px;padding:0px">
<li style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"><p style="margin:0px 0px 1em;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;clear:both">Create a channel for the user.</p>
</li><li style="margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent"><p style="margin:0px 0px 1em;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;clear:both">
Send messages using <code style="margin:0px;padding:1px 5px;border:0px;vertical-align:baseline;background-color:rgb(238,238,238);font-family:Consolas,Menlo,Monaco,'Lucida Console','Liberation Mono','DejaVu Sans Mono','Bitstream Vera Sans Mono','Courier New',monospace,serif">gproc:send/2</code> on state change(again an iterative process)</p>
</li></ol><p style="clear:both;vertical-align:baseline;line-height:17.600000381469727px;font-size:13.600000381469727px;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 1em;border:0px;padding:0px">
It seems to me in both cases looping can not be avoided. So I am leaning towards second approach myself as there is less over head(single channel for every user no subscriptions)</p><p style="clear:both;vertical-align:baseline;line-height:17.600000381469727px;font-size:13.600000381469727px;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 1em;border:0px;padding:0px">
Which approach is better?</p><h3 style="vertical-align:baseline;line-height:1.3;font-size:15.199999809265137px;font-family:'Trebuchet MS','Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 1em;word-wrap:break-word;border:0px;padding:0px">
Relevant article</h3><p style="clear:both;vertical-align:baseline;line-height:17.600000381469727px;font-size:13.600000381469727px;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 1em;border:0px;padding:0px">
<a href="https://www.facebook.com/note.php?note_id=14218138919" rel="nofollow" target="_blank" style="color:rgb(74,107,130);margin:0px;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;text-decoration:none">Facebook chat blog</a></p>
<blockquote style="vertical-align:baseline;line-height:17.600000381469727px;quotes:none;font-size:13.600000381469727px;background-color:rgb(238,238,238);font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 10px;border:0px;padding:10px 10px 1px">
<p style="margin:0px 0px 1em;padding:0px;border:0px;vertical-align:baseline;background-color:transparent;clear:both">The naive implementation of sending a notification to all friends whenever a user comes online or goes offline has a worst case cost of O(average friendlist size * peak users * churn rate) messages/second, where churn rate is the frequency with which users come online and go offline, in events/second. This is wildly inefficient to the point of being untenable, given that the average number of friends per user is measured in the hundreds, and the number of concurrent users during peak site usage is on the order of several millions.</p>
</blockquote><p style="clear:both;vertical-align:baseline;line-height:17.600000381469727px;font-size:13.600000381469727px;font-family:Arial,'Liberation Sans','DejaVu Sans',sans-serif;margin:0px 0px 1em;border:0px;padding:0px">
With regard to above comment my question is the same. If you use pub/sub you save on sending notification but incur cost on subscribing in the first place so how does pub/sub benefit over normal sending.</p></div>