How to force a server to send an event-data constantly, using ASP.NET MVC + SignalR?

Andrey Kotov

ExportClient class has OnTickRecieved event, which helps me to receive some data (bid prices from market). All I want - is to receive this data real-time on my charts in browser. When I press Go button on UI-side, it calls Go() method in controller, and then nothing is happening. And it's logical - because after request on server, controller is destroyed.

My question is: how to force a server to send me an event-data constantly?

Controller code:

public class ChartsController : Controller
{
    [HttpGet]
    public void Go()
    {
        var exportClient = new ExportClient();
        exportClient.TickRecieved += exportClient_TickRecieved;
    }

    private void exportClient_TickRecieved(object sender, TickRecievedEventArgs args)
    {
        ImpulserHub.SendBidPrice(args.Bid);
    }
}

Hub code:

[HubName("impulserHub")]
public class ImpulserHub : Hub
{
   public static void SendBidPrice(double bid)
   {
       var hubContext = GlobalHost.ConnectionManager.GetHubContext<ImpulserHub>();
       hubContext.Clients.All.sendBidPrice(bid);
   }
}

And I have tested SignalR, this code works fine:

[HttpGet]
public void Go()
{
   ImpulserHub.SendBidPrice(3.3333333); // I have received this number on UI
}
Carl St-Laurent

The simplest way would be to have your export client as a singleton or as a static variable and register to you event in the global scope (probably in your Application_Start() method from Global.asax.cs). Your hub code should also be moved out since hubs are transient like controllers.

This is how it would look like:

private ExportClient _exportClient;
private IHubContext _impulserHub;

protected void Application_Start()
{
    _exportClient = new ExportClient();
    exportClient.TickRecieved += exportClient_TickRecieved;
    _impulserHub = GlobalHost.ConnectionManager.GetHubContext<ImpulserHub>();
}

private void exportClient_TickRecieved(object sender, TickRecievedEventArgs args)
{
    _impulserHub.Clients.All.sendBidPrice(args.Bid);
}

There is still an issue remaining with this code. IIS will tear down websites that are not actively receiving requests. This means that the code may stop working at any time even if the event is triggered. Managing application teardown is hard since the state must be saved/transferred between application start and stop. Unless you can set IIS never to tear down your application (most of the time impossible on shared or cloud hosting), you should try to use the HangFire library which is available as a nuget package.It was specifically designed for that use case and with a bit of refactoring, your code could look like that:

private ExportClient _exportClient;
private IHubContext _impulserHub;
protected void Application_Start()
{
    _exportClient = new ExportClient();
    exportClient.TickRecieved += exportClient_TickRecieved;

    _impulserHub = GlobalHost.ConnectionManager.GetHubContext<ImpulserHub>();

    BackgroundJob.Schedule(() => _impulserHub.Clients.All.sendBidPrice(_exportClient.GetBid()), TimeSpan.FromSeconds(5));
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to convert JS Object to JSON using knockoutjs utility to send the json data to server in asp.net mvc?

From Dev

disconnect event on Signalr Asp.net mvc

From Java

How to send checked rows to server via Ajax using ASP.NET MVC?

From Dev

How to send data from javascript to ASP.NET MVC controller using URL

From Dev

Insert Excel data to SQL server using ASP.NET MVC

From Dev

Send EMail using asp.net mvc

From Dev

How to cache data on server in asp.net mvc 4?

From Dev

Force file download in a browser using ASP.Net MVC when the file is located on a different server without downloading it on my server first

From Dev

Using SignalR as a standalone server application, and not an Asp.Net one

From Dev

How to post data to ASP.NET MVC controller using AJAX?

From Dev

How to cache data asp.net mvc using Cache obect

From Dev

How to load data using thread in ASP.Net MVC?

From Dev

How to send big data via SignalR in .NET client

From Dev

How to force the user to login to see any content using ASP.NET MVC 4

From Dev

How to force the user to login to see any content using ASP.NET MVC 4

From Dev

How can I prevent submit event and with onclick event call ActionResult Method Create to submit my form data using Jquery ASP.NET MVC?

From Dev

Send notification to web client from external service via SignalR hub hosted in Asp.Net MVC Application

From Dev

How I do bind data to DisplayFor from event blur on asp.net mvc

From Dev

How can i send a object from Asp.net MVC controller to Asp.Net WebForm without using Session?

From Java

How to send two arrays simultaneously to view using JSON asp.net MVC

From Dev

How can I send email using Gmail SMTP in asp.net mvc application?

From Dev

How to send an email on Postal using an email address from the database Asp.Net MVC

From Dev

How to send an email to multiple email addresses in the database using Postal in ASP.NET MVC

From Dev

How to send hidden ID value FormData using asp.net MVC?

From Dev

jquery ajax send data - ASP.Net MVC

From Dev

How to hide and show div in asp.net mvc 5 using dropdownlist change event

From Dev

ASP.Net MVC 5 + SignalR + Ninject

From Dev

SignalR with ASP.NET MVC and WinForms

From Dev

OwinStartup and startup in signalr in asp.net mvc

Related Related

  1. 1

    How to convert JS Object to JSON using knockoutjs utility to send the json data to server in asp.net mvc?

  2. 2

    disconnect event on Signalr Asp.net mvc

  3. 3

    How to send checked rows to server via Ajax using ASP.NET MVC?

  4. 4

    How to send data from javascript to ASP.NET MVC controller using URL

  5. 5

    Insert Excel data to SQL server using ASP.NET MVC

  6. 6

    Send EMail using asp.net mvc

  7. 7

    How to cache data on server in asp.net mvc 4?

  8. 8

    Force file download in a browser using ASP.Net MVC when the file is located on a different server without downloading it on my server first

  9. 9

    Using SignalR as a standalone server application, and not an Asp.Net one

  10. 10

    How to post data to ASP.NET MVC controller using AJAX?

  11. 11

    How to cache data asp.net mvc using Cache obect

  12. 12

    How to load data using thread in ASP.Net MVC?

  13. 13

    How to send big data via SignalR in .NET client

  14. 14

    How to force the user to login to see any content using ASP.NET MVC 4

  15. 15

    How to force the user to login to see any content using ASP.NET MVC 4

  16. 16

    How can I prevent submit event and with onclick event call ActionResult Method Create to submit my form data using Jquery ASP.NET MVC?

  17. 17

    Send notification to web client from external service via SignalR hub hosted in Asp.Net MVC Application

  18. 18

    How I do bind data to DisplayFor from event blur on asp.net mvc

  19. 19

    How can i send a object from Asp.net MVC controller to Asp.Net WebForm without using Session?

  20. 20

    How to send two arrays simultaneously to view using JSON asp.net MVC

  21. 21

    How can I send email using Gmail SMTP in asp.net mvc application?

  22. 22

    How to send an email on Postal using an email address from the database Asp.Net MVC

  23. 23

    How to send an email to multiple email addresses in the database using Postal in ASP.NET MVC

  24. 24

    How to send hidden ID value FormData using asp.net MVC?

  25. 25

    jquery ajax send data - ASP.Net MVC

  26. 26

    How to hide and show div in asp.net mvc 5 using dropdownlist change event

  27. 27

    ASP.Net MVC 5 + SignalR + Ninject

  28. 28

    SignalR with ASP.NET MVC and WinForms

  29. 29

    OwinStartup and startup in signalr in asp.net mvc

HotTag

Archive