Click here to Skip to main content
15,889,116 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
i want to send the server event(push notificaton) to specific users(based on their usernames) any idea how to do it?
Posted
Updated 16-Jun-12 21:46pm
v3
Comments
bbirajdar 16-Jun-12 16:08pm    
Not clear....
agha_ali22 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)

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#
C#
string JSON = "data: {'IntendedFor' = 'AdminClerks', 'Message' = 'You\'ve got a server event'}";
Response.Write(JSON+'\n\n');
Response.Flush();


Client HTML
HTML
<input id='myGroup' type='hidden' value='AdminClerk' />


Client Javascript
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");
}
 
Share this answer
 
v2
Comments
agha_ali22 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 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.
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[^]
 
Share this answer
 
Comments
agha_ali22 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?
bbirajdar 18-Jun-12 6:02am    
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 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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900