SignalR with MVC - asp.net-mvc

The requirement in our project is, Web UI(MVC 5) will place a request to adapter(intermediate layer) which takes 3-4mins to process the request and computes response. This response needs to be pushed back to UI. We are planning to implement Signalr for posting back the response to UI. I did a small POC on SignalR to open connection and to call a method from javascript and get response back. how can I push the data from server to client(once connection is set), no call from javascript/Web for requesting the response?.

To push datas from the server to the client the basic usage is :
On the client side :
var myHub = $.connection.CustomHub;
myHub.client.myFunction= function (param) {
alert(param);
};
On the server side :
Clients.All.myFunction("parameter");
To find more infos about signalr take a look here :
http://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr
You can download the light chat project in the page it's a good start.

Get Hubcontext of the application
IHubContext hubContext = GlobalHost.ConnectionManager.GetHubContext('HubName');
On top of hubcontext can broadcast message to all the clients or groups or to specific client based on connection Id
hubContext.Clients.Client(connectionId).JavaScript function(data)

Related

How do you forward Twilio calls to different URLs in the middle of a call? (Using Node)

I'd like the following functionality with Twilio/Node: either
my server receives incoming call and the call rings on our custom client; at some point during the call (ideally can work before answering or after answering) or if no one answers on the client, the call is transferred to a webhook on a different server (my CRM provider) so the CRM can deal with it. OR
same as above, but the incoming call posts the incoming call request to both my server & my CRM webhook for the incoming call; I think this might not be possible though, not sure
I'm able to receive a Twilio call on my server without problem, and able to receive Twilio calls in my CRM without problem. However, when I tried to forward a call to the CRM after first receiving it on my custom server/client, it seems to always disconnect abrubtly. Pls help!
The code I'm using to update the call is below. The url works normally if sending the call directly to the CRM webhook. The CallSid is from my custom client from the incoming call
client.calls(req.body.CallSid)
.update({method: 'POST', url: 'https://crm.crmprovider.com/ctiapi/xml/cticall/twilio?authtoken=abc'})
Appreciate any help!
Think I figured out the proper way to do this. Should be using an "action" with "dial" and then checking "DialCallStatus" in the action endpoint and dealing with various statuses as appropriate. Sample code:
// On server, receive call. This is the url Twilio is
// set to post webhook to when call comes in
app.post('/incomingCall', (req, res) => {
const twiml = new VoiceResponse();
// Dial client first; after, call /callAction
// I believe this will call /callAction if call is unanswered for 10 seconds or after a completed call or if there's no client open
const dial = twiml.dial({timeout:10, action: '/callAction'});
const client = 'whateverClientName'
dial.client(client)
res.type('text/xml')
res.send(twiml.toString())
})
app.post('/callAction',(req,res)=>{
const twiml = new VoiceResponse();
// Can set below if for other things like if call not completed, answered, or cancelled
// In this example we say if call's not completed, route call to 3rd party site's webhook to further deal with the ongoing call
if(req.body.DialCallStatus!=='completed'){
twiml.redirect({method: 'POST'},'https://thirdpartywebhook.com/abc')}
else {twiml.hangup()}
res.type('text/xml')
res.send(twiml.toString())
})
I didn't find Twilio docs super straightforward on this so hopefully this helps someone in the future!

Slow Startup for setup SignalR connection

Setup a simple SignalR Chat web application using the Microsoft tutorial code.
!--SignalR script to update the chat page and send messages.-->
<script>
$(function () {
// Reference the auto-generated proxy for the hub.
var chat = $.connection.chatHub;
// Create a function that the hub can call back to display messages.
chat.client.addNewMessageToPage = function (name, message) {
// Add the message to the page.
$('#discussion').append('<li><strong>' + htmlEncode(name)
+ '</strong>: ' + htmlEncode(message) + '</li>');
};
// Get the user name and store it to prepend to messages.
$('#displayname').val(prompt('Enter your name:', ''));
// Set initial focus to message input box.
$('#message').focus();
// Start the connection.
$.connection.hub.start().done(function () {
$('#sendmessage').click(function () {
// Call the Send method on the hub.
chat.server.send($('#displayname').val(), $('#message').val());
// Clear text box and reset focus for next comment.
$('#message').val('').focus();
});
});
});
It's running OK when debugging locally, I can send message immediately after putting in a username. But when deployed onto the Azure, after entering the username, I have to wait like 5 seconds before I can submit a new message (no response clicking Send button), but after this first message, for all the following messages I can send instantly.
For me, it looks like it is slow when setting up the initial connection ($.connection.hub.start()).
Is this normal? How can I improve the performance of this simple application?
By default websockets are not enabled on Azure and, by default, the client tries different transports starting from webSockets. If websockets does not work it will fall back to serverSentEvents and finally to longPolling. This takes time. Make sure you turn on websockets on Azure or specify that you want to use only serverSentEvents and longPolling transports.

SignalR is only sending the first two messages

I have a .Net MVC web application and I am using SignalR to implement a progress bar functionality.
I have a View that is making an Ajax POST to an action using JQuery:
$.ajax({
url: actionUrl,
type: 'POST',
data: { ids: ids },
success: function (data) {
...
}
});
The Controller is procesing information inside a loop, and every n iteration is sending a message using signalr to a Hub. The client is connected to the Hub and updates a progress bar with the information in the messages.
I open the connection to the signalr hub with this code:
var connection = new HubConnection("http://localhost/");
connection.Credentials = CredentialCache.DefaultNetworkCredentials;
this.proxy = connection.CreateHubProxy("progressHub");
connection.Start().Wait();
And then send the information with this:
proxy.Invoke("ProgressChanged", taskId, progress);
This is working correctly for the first two updates, but from that on it is not working anymore until the end of the long process, when I receive all the remaining messages. I am receiving something like this:
0% complete
5% complete
... (long pause as the process completes)
10%, 15%, 20%, 25%, etc (all these messages come together)
It is always the first two messages, not a random number of messages.
Do you know any configuration that I may be missing?
I tried adding a sleep after each message and making the action async and adding .Wait() to each message, but it is always the same behaviour.
I tried it in IISExpress and full IIS 8.
Your action method in the controller should be async and call an asynchronous Task. My guess is that while your ajax call is waiting for completion, the call made by signalR are probalby queued until your proccess is complete and end the ajax request.

SignalR, Messages are to be pushed by the Server without any event triggered by the client

There are running examples of SignalR, but in those, i have seen that the process is started by the client i.e. every piece of code contains following similar lines
$.connection.hub.start().done(function () {
$('#mybutton').click(function () {
notifier.server.doLongOperation();
});
});
The process on server starts on $('#mybutton').click and then responds.
Is my understanding correct? If yes then is it possible to start the process by Server? I mean Server will push messages to all clients without any triggering from the client side.
This didn't work
var context = GlobalHost.ConnectionManager.GetHubContext<Broadcast>();
context.Clients.All.Send(message);
My bad, method name on client side was incorrect. Problem solved
Yes it is possible to send server initiated "messages" from the server to clients. Note that you have to call a method on the client. Note that it's a RPC/Remoting type of communication.
On the server you'd have a code like this:
Clients.All.Say("Hello World!");
where the client needs to define a function:
myHub.client.say = function (message) {
console.log(message);
});
see the SignalR documentation

How SignalR manages connection between Postbacks

1> Just want to understand how SignalR 1.x functions in a particular scenario
Lets say we have a 10 clients connected to Hub and one of the connected clients say client-1 performs a postback so OnDisconnected is called than OnConnected is called right ?
What happens if during this phase if client-2 try's to send message to client-1 exactly between the said scenario ie (msg is sent after client-1 is disconnected and before connected again )will client-1 miss the message or there's internal mechanism which makes sure client-1 does not miss the message sent by client-2
2> Second query I have is that I'm trying to pass a querystring using following code
var chat = $.connection.myHub;
$.connection.myHub.qs = { "token": "hello" };
but not able to retrieve it on the server side from the Context object
using
Context.QueryString.AllKeys
I even tried
var chat = $.connection.myHub;
$.connection.myHub.qs = "token=hello" ;
But it does not work ie when I check the keys, token is not present in AllKeys
Will appreciate if someone just help me out.
1: If a postback occurs a client will disconnect and then connect. However, when the client performs a connect again it will have a different Connection Id than it had prior to the postback. Therefore, any message sent to the old connection id will be missed because when the users browser connects again it will be known as a different client.
2: You're trying to set the query string on the hub proxy, not the connection. What you should be doing is:
$.connection.hub.qs = { foo: "bar" };

Resources