Using Signalr with Asp.net MVC to create simple chat application part II

As we mentioned in the previous example here, we can create a simple asynchronous actions with the signalr library like chat, real-time streaming and so on. in this blog i am going to go with another library from signalr its PersistentConnection. A PersistentConnection is the base class that has an api for exposing a SignalR service over http.

we are going to complete in the the previous application we created

Step 1:Add AdvChatController

we are going to create a new controller called AdvChatController with two action methods Index and Adv. then add index view

Step 2: Create Signalr PersistentConnection

Create a class the derives from PersistentConnection

Step 3:Setup Routing

Make a route for your connection in Global.asax

If you are setting up other routes, the SignalR mapping needs to be done before the other ones.

Step 4: Adding receiving functionality in MyConnection

Add the following functions to MyConnection class

here as you notice we can manage the users that are connected to server first on connect action that action will be called when the user connects to the application so we can add the user to a certain group also on disconnect action that action will be called when user leaves the page so we can remove the user from group. also on received function to perform the sending action to certain group

step 5: Adding Client code

Append javascript references to the head of the page as follows

Append the following javascript

Append the following Html to your page

that’s all you can now run the application now in multiple browsers and test it

You can download the sample from here