Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
i want to send the server event(push notificaton) to specific users(based on their usernames) any idea how to do it?
Posted 16-Jun-12 9:19am
Edited 16-Jun-12 22:46pm
v3
Comments
BRAHMA Brahmanand Kumar Birajdar at 16-Jun-12 16:08pm
   
Not clear....
agha_ali22 at 17-Jun-12 3:13am
   
i have an aspx page which send notification to connected clients (connected by Html5 event source) and now i wanted to send notification to a specific username which i have saved in session or in some scenario i want to send notification to a group which is also saved in session as groupid(admin(1),manager(2),e.t.c)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

HTTP works in disconnected mode. The server does not remember its clients.
 
You should implement server polling from client side scripting
 
http://api.jquery.com/category/ajax/[^]
 
http://stackoverflow.com/questions/3583203/server-polling-with-javascript[^]
 
http://stackoverflow.com/questions/1155245/javascript-poll-server-will-this-cause-a-stack-overflow[^]
  Permalink  
Comments
agha_ali22 at 18-Jun-12 5:24am
   
what i want is to notify a group of clients that something happened , now i can notify all the connected clients what i want is notify a group
 
Here is the Default.aspx file<script type="text/javascript">
 
window.onload = function () {
if (window.EventSource == undefined) {
document.getElementById('targetDiv').innerHTML = "Your browser doesn't support Server Side Events.";
}
var source = new EventSource('SSEHandler.aspx');
 
source.onopen = function (event) {
document.getElementById('targetDiv').innerHTML += 'Connection Opened.<br>';
};
 
source.onerror = function (event) {
if (event.eventPhase == EventSource.CLOSED) {
document.getElementById('targetDiv').innerHTML += 'Connection Closed.<br>';
}
};
 
source.onmessage = function (event) {
$("#targetDiv").append(event.data);
};
}
</script>
 

<div id="targetDiv">
</div>
 

and here is 'SSEHandler.aspx' cs
 
protected void Page_Load(object sender, EventArgs e)
{
 
Response.ContentType = "text/event-stream";

while (true)
{
if(checksomecondition())
 
{
Response.Write(string.Format("data: {0}\n\n","Hello"));
Response.Flush();
}
i++;
System.Threading.Thread.Sleep(3000);
}
}
 
now i want to send the response to the desired user. this can be done by long pooling but i want to do with html5 Server sent event any idea?
   
The reverse is not possible because of the stateless architecture of the HTTP. Polling is the only option as far as I know..
agha_ali22 at 18-Jun-12 6:40am
   
Server Side Events is a more basic way to send one way data from the server to the client. It works like a long request from the clients JavaScript function where data can is returned bit by bit during the response and each time a bit is returned a JavaScript event is triggered.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

SSE broadcasts to all open connections. Include your group in your JSON stream and do the group check in the javascript.
 
Note : This is not a secure method, do not use sensitive data this way, since everyone gets every single message, it'll only be handled by your javascript differently.
 
Example :
 
Server-Side C#
string JSON = "data: {'IntendedFor' = 'AdminClerks', 'Message' = 'You\'ve got a server event'}";
Response.Write(JSON+'\n\n');
Response.Flush();
 
Client HTML
<input id='myGroup' type='hidden' value='AdminClerk' />
 
Client Javascript
var myGroup = document.GetElementById('myGroup').value;
var source = new EventSource('<insert url="" here="">');
source.onmessage = function (e) {
  var myJSONResponse = JSON.parse(e.data);
  if (myJSONResponse.IntendedFor == myGroup)
    alert("You've got a server event");
}
  Permalink  
v2
Comments
agha_ali22 at 16-Apr-13 12:25pm
   
Now I am using SignalR which exactly do what i want,but as my question was concerned your answer is correct
D Hinterlang at 17-Apr-13 3:40am
   
I'm also using SignalR :) Really awesome framework for doing exactly this sort of thing.
 
Thanks for solution accept :) It's how I was doing it before I moved to SignalR.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 370
1 Sergey Alexandrovich Kryukov 329
2 BillWoodruff 235
3 Afzaal Ahmad Zeeshan 209
4 CPallini 195
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 4 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100