I am using .net project and create one web page to use with SignalR. This code below working with one web page. Client side:
$.connection.hub.start().done(function () { });
var chat = $.connection.chatHub;
chat.client.broadcastMessage = function (name, message) { };
Server side:
var context =
Microsoft.AspNet.SignalR.GlobalHost.ConnectionManager.GetHubContext<SignalRChat.ChatHub>();
context.Clients.All.broadcastMessage(name, message);
Then I create second web page and copy client and server code from page 1.
Problem is that when I call
context.Clients.All.broadcastMessage(name, message);
from second page. it fired function
chat.client.broadcastMessage
on first page. How can I fixed that to fired client side for page 2?
Thanks
Do you want it to fire on both pages -
I suspect you did not create a connection on the page 2. Once you have a valid connection on page 2 it would fire on both page 1 and 2 if you had both open. Or only on the page you have open.
If you want it to fire on each page but not both pages you have some changes to make. You have several options you can go with here with no additional context:
Really simple - rename chat.client.broadcastMessage on page 2 to something else and call it from the server with a new method.
Look into Groups - updating context.Clients.Group(groupname).broadcastMessage(name, message) you can send messages to users on page1 (assuming you have a group for page 1 and page 2) and then only send messages to users on page 2 when needed.
Related
I'm playing with different ways to create push notifications in asp.net core, since my boss asked me to research it.
So i just set up a barebones project that uses signalr to send messages from a hub to the client, and conversely for the client to the hub.
Currently, I have a button on a certain page where this javascript is used:
function receive() {
$.ajax({
url: '/msg/SendToClient',
type: 'get',
});
}
var connection = new signalR.HubConnectionBuilder()
.withUrl("/NotificationHub")
.build();
connection.on("ReceiveMessage", function (user, message) {
alert(message);
});
connection.start();
Where the receive() is called everytime the button is pushed, that means that a corresponding controller action is called, which sends a message from server -> client.
But what I want is that I would like the javascript file to be laying on the client, and just run once in a while to check for new available push notifications.
A few scenarios are available, and I would like to know about all of them:
Push notifications when browser is closed. I'm guessing this is not possible with a "regular" service worker?
Push notification when browser is open, but site is not open. For this, I guess I will need to somehow have a javascript file/service worker on the clients computer just always running. This I really have no idea how to achieve this.
A push notification that can work when the site is open in the browser. All I think I need for this is to have the javascript included always when the user is on the site, and then somehow make a request every few minutes.
Any inputs on these three things will be greatly appreciated. Is any of them easily achieveable/advisable? Is there any "normal" mode of operation for these three approaches to push notifications?
For any of these three scenarios, is there a case where signalr is parhaps not the best resource?
In my MVC5 application, users log in from external software without a separate login. After some times the external application can be closed and in this case a login form should appear.
I receive an event
Software.DataChange + = delegate ()
{
.... code goes here
IsUserLoggedIn = false;
};
And now the globally defined variable IsUserLoggedIn changes.
Now the login view should be called. And that's where my problems begin.
Can I do this in the controller?
Or can I assign a field via SignalR and then trigger a reload of the page? SignalR is already used for some progress bars.
Thanks
Ottilie
I am using Meteor with Iron Router and Stripe. Everything is working great, but I cant figure out how to re-direct user to a final order complete page after the Stripe charge is completed.
On my client side I have a modal box that appears which contains a button that says, "Pay" When the Pay button is clicked an event is fired that calls up and opens Stripe Checkout.
The Stripe Checkout then initiates on the client and the user is able to enter the card details and submit the payment. The server side method for charging the card thru Stripe is completed and I also have some other basic database tasks that are being performed to log the result and complete the order status.
I have created a route using Iron router that I want the user to be re-directed too after the Stripe Payment is completed.
As of now the modal box continues to stay on the screen. I am trying to make the Router.go send user to the order page that had been setup after the order is finished.
I beleive the Iron Router Router.go is used client-side only. How can I complete the order process and make the client-side modal box disappear after the Stripe charge is completed and re-direct user to a final complete page.
When I use the Router.go on server-side I am getting error:
Exception in callback of async function: TypeError: Object function router(req, res, next) {//XXX this assumes no other routers on the parent stack which we should probably fix
You can't force the user's browser away from it's page if you don't already have some logic on the page to "remote control it". Fortunately there are a number of options for the latter:
If you are using a router you could make the route itself reactive, depending on the content of a subscribed collection. Alternatively, you can have an Tracker.autorun or an observeChanges block on your client side code that checks for changes to that "control" collection and then execute Router.go accordingly.
As for the control collection, a simple collection like:
var Control = new Mongo.Collection('control');
would do, and then you insert into it from the server when the event occurs (Control.insert({route: "newroute"})), and check for content changes on the client, for instance like so:
Control.find().observeChanges({
added: function(id, doc) {
Router.go(doc.route);
});
I am building a single page app which uses sammy.js, knockout.js and SignalR. The main page (index.html) loads additional html pages into a div based upon the client side route.
I have 2 SignalR hubs, one is connected to by the initial page for server side push data and this works fine. However one of the pages which are loaded when the user navigates to it should also connect to a different hub.
In the main page I am doing the following:
window.hubReady = $.connection.hub.start()
var hub1 = $.connection.hub1;
hub1.updateReceived = function () {
alert('data from server');
}
window.hubReady.done(function() {
hub1.server.start();
});
In the second page I have:
var hub2 = $.connection.hub2;
hub2.updateReceived = function () {
alert('data from server');
}
window.hubReady.done(function() {
hub2.server.start();
});
However I never receive any updates in the second page.
Any idea where I am going wrong?
In order to receive updates from a hub you must have at least 1 client side function declared for that hub when the connection is started. Judging from the libraries you are using I'm assuming you have a single page application and therefore don't instantiate your hub2 data until the connection has already started.
So an easy fix would be to just declare a hub2 client side function alongside your hub1 client side function before start is called. If you want to add more client side functions after the connection has started you'll have to use the .on method.
AKA:
hub2.on("updateReceived", function () {
alert("data from server");
});
I have created a lib called SignalR.EventAggregatorProxy.
it proxies between server side domain events and client side code. Its designed with MVVM and SPA in mind and takes care of all the hub plumbing. Check the wiki for how to set it up.
https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/wiki
once its configured all you need to subscribe to a event is
ViewModel = function() {
signalR.eventAggregator.subscribe(MyApp.Events.TestEvent, this.onTestEvent, this);
};
MyApp.Events.TestEvent corresponds to a server side .NET event. You can also constraint which event should go to which usera
I'm trying to find examples on how to get real time updates using a web service in ASP.NET MVC (Version doesn't matter) and posting it back to a specific user's browser window.
A perfect example would be a type of chat system like that of facebooks' where responses are send to the appropriate browser(client) whenever a message has been posted instead of creating a javascript timer on the page that checks for new messages every 5 seconds. I've heard tons of times about types of sync programs out there, but i'm looking for this in code, not using a third party software.
What i'm looking to do specifically:
I'm trying to create a web browser chat client that is SQL and Web Service based in ASP.NET MVC. When you have 2-4 different usernames logged into the system they chat and send messages to each other that is saved in an SQL database, then when there has been a new entry (or someone sent a new message) the Web Service see's this change and then shows the receiving user the new updated message. E.G Full Chat Synced Chat using a Web Service.
The thing that really stomps me in general is I have no idea how to detect if something new is added to an SQL table, and also I have no idea how to send information from SQL to a specific user's web browser. So if there are people userA, userB, userC all on the website, i don't know how to only show a message to userC if they are all under the username "guest". I would love to know hot to do this feature not only for what i'm trying to create now, but for future projects as well.
Can anyone point me into the right direction please? I know SQL pretty well, and web services i'm intermediate with.
You can use SignalR for this task.
Via Scott Hanselman:
Create Asp.net mvc empty application
install nuget package of SignalR
Add new Controller (as example HomeController):
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
Create view Index with javascript references:
#Url.Content("~/Scripts/jquery-1.6.4.min.js")"
"#Url.Content("~/Scripts/jquery.signalR.js")"
and function:
$(function () {
var hub = $.connection.chatHub;
hub.AddMessage = function (msg) {
$('#messages').append('<li>' + msg + '</li>');
};
$.connection.hub.start().done(function() {
$('#send').click(function() {
hub.send($('#msg').val());
});
});
});
Create class ChatHub:
public class ChatHub:Hub
{
public void Send(string message)
{
Clients.AddMessage(message);
}
}