Skip to main content

Posts

Showing posts with the label signalr

How to create chat application using microsoft asp.net signalr

open visual studio and create a new MVC application
Select Empty MVC application

Open Package Manager Console and install SignalR

Right click on project and add new class ChatHub and inherit from Hub class that resides in Microsoft.AspNet.SignalR

public class ChatHub:Hub { //this method is called when client send message public void sendMessage(string message) { //this method brodcast message to all connected client Clients.All.newMessage(message); } } Right click on project and add new class named Startup and add following code inside it
public class Startup { public void Configuration(IAppBuilder app) { app.MapSignalR(); } } Create a new html page and add following markup inside it.You can see that I have added three JavaScript lib :jquery,jqery-signalr* and  signalr/hubs.Signalr/hubs contains functions for interacting with server side hub class and also it generated automatic…

Access to the Hubs from outside of the Hub class

The Clients dynamic property of the Hub gives you access to all clients connected to the hub within the hub class. However, what if you would like to push data to the clients from outside of the Hub class.Use following code snippet and replace HubClass with your own class and get access of the signalr clients and server dynamic properties/methods.var myHub = GlobalHost.ConnectionManager.GetHubContext<HubClass>();