From the course: Building Real-Time Web Apps with SignalR 2

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Adding private messages to Fun Chat

Adding private messages to Fun Chat

From the course: Building Real-Time Web Apps with SignalR 2

Start my 1-month free trial

Adding private messages to Fun Chat

- [Instructor] We want to add a private messaging feature into Fun Chat. Now, SignalR has no direct means of sending a message to an individual user without knowing their connection ID which is randomly assigned every time a user connects. A simple way to achieve this is to use a concept called single user groups. Basically we're going to assign a user to a unique group based on their own user name whenever they connect. This will allow us to send messages to individual users as long as we know their user name. Let's set this up now. We'll start in our chat hub, and we're going to add a unconnected hook. (clicking) Public override. (clicking) Unconnected. In the body, we'll grab the user name of the currently authenticated user (clicking) from context.user.identity.name. (clicking) Remember this will be null if the user is not authenticated, so we'll check for that. We'll say if the username (clicking) is not null or empty (clicking) then we'll add the user to their single user group,…

Contents