Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8991991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:51:12+00:00 2026-06-15T22:51:12+00:00

In my chat application project, I am trying to broadcast usernames to all the

  • 0

In my chat application project, I am trying to broadcast usernames to all the users whenever a new user is connected to a server. and remove a username whenever the user leaves the server. The below is the code which I have tried by going through tutorials. (please check the file.js file which is not showing the desired output)

Chat.cs (Working) –> Implements “Hub” class of SignalR

public class Chat : Hub 
{ 
    /* showing only related content */
    static ConcurrentDictionary<string, User> _users = new ConcurrentDictionary<string, User>();

    public override Task OnDisconnected()
    {            
        var user = _users[Context.ConnectionId]; //user as ConnectionId
        User removedUser; //new class object
        _users.TryRemove(Context.ConnectionId, out removedUser);
        return Clients.All.leave(user, DateTime.Now.ToString()); //dynamic expression
    }

    public void Joined()
    {
        User user = new User(Context.ConnectionId, Clients.Caller.username);
        _users.TryAdd(user.ConnectionID, user);
        Clients.All.joins(user.ConnectionID, user.Name, DateTime.Now); //dynamic expression
    }
}

User.cs (Working)

public class User
{
    public User(string ConnID, string Username)
    {
        Name = Username;
        ConnectionID = ConnID;
    }
    public string Name { get; set; }
    public string ConnectionID { get; set; }
}

file.js (not working)

var chatUsername = window.prompt("Enter Username:", "");  //username
var chat = $.connection.chat;  //connection
//
chat.client.joins = function (ConnectionId, name, Date) {
    ConnectionId = 1; /* given value to just test */
    name = chatUsername;
};

chat.client.leave = function (user, date) {
    user = "";  //making the string empty so that the disconnected user value will be lost.
};

//Here is the connection which calls the "Joined" function of the server (Chat.cs)

What should I write in file.js functions (joins and leave) so that I will get the desired result as I mentioned above. Before asking here, I have gone through this site which is doing the same but including additional javascript files(knockout.js and json) which I dont want to include.(bcos I am new to jquery).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:51:13+00:00Added an answer on June 15, 2026 at 10:51 pm

    In order to pass UserNames to the client you can take your dictionary and in your joined server side method you could change the SignalR line to be:

    Clients.All.joins(_users.Values); //dynamic expression
    

    Then the client version of joins would be:

    chat.client.joins = function (users) {
        for(var i = users.length - 1; i >= 0; i--) {
            alert("User Name: " + users[i].Name + "\nUser Connection ID: " + users[i].ConnectionID);
        }
    };
    

    Of course you can handle the user information differently than alerting it, but that’s the gist of how to handle the data. Lastly, I’d recommend against passing down the connection ID to everyone because a third party could then easily hijack it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my Chat application project, I am trying to restrict spaces when the user
I am working on a mini-project which consists of doing a client-server chat application.
I am new to asp.net. Here I am trying to create a chat application.
I have a school project in which I have to implement a chat application,
I am developing an chat application. The message display is all html, so html
I have designed a chat application where different users can create the account, but
I'm new to android application development. I tried to develop an android server client
I am trying to run a sample bluetooth chat application onto my smartphone (running
I'm writing a chat application as part of a project which is a sort
I have written a Chat application using C#. The project is a separate Windows

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.