Home > Software design >  Clients not receiving message in signalr
Clients not receiving message in signalr

Time:09-17

my signalr connected clients are not receiving messages. my chathub:

public class Chathub : Hub
    {
        private readonly string _botUser;
        private readonly string _globalRoom;
        public Chathub()
        {
            _botUser = "My Chatbot";
            _globalRoom = "Lobby";
        }
        public async Task Join(string message)
        {
            //await Groups.AddToGroupAsync(Context.ConnectionId, _globalRoom);
            //await Clients.Group(_globalRoom).SendAsync("ReceiveMessage", $"{_botUser} : Welcome {message}");
            await Clients.All.SendAsync("ReceiveMessage", $"{_botUser} : Welcome {Context.ConnectionId}"); //Checking connectionid
            //await Clients.All.SendAsync("ReceiveMessage", $"{_botUser} : Welcome {message}");
        }
    }

I've defined my hubconnection in SharedService:

export class SharedService {

  public readonly URL = "https://localhost:44333/";
  private hubConnection: HubConnection;

  messageModel: Message = {
    message: null
  }

  constructor(private http: HttpClient) {
    this.BuildConnection();
    this.StartConnection();
    this.JoinMessage();
  }

  public BuildConnection = () => {
    this.hubConnection = new HubConnectionBuilder().withUrl('/chathub').build();

  }

  public StartConnection = () => {
    this.hubConnection.start()
      .then(() => console.log("Connection started...."))
      .catch(err => console.log(err));
  }

  LoginUser(UserModel: User): Observable<User> {
    this.hubConnection.invoke("Join", UserModel.userName);
    return this.http.post<User>(this.URL   "Account/LoginUser", UserModel);
  }

  JoinMessage(): Observable<string> {
    this.hubConnection.on("ReceiveMessage", (messages) => {
      this.messageModel.message = messages;
    });
    return of(this.messageModel.message);
  }

I have two compenents. As soon as user logs in, hubconnection is started and is routed to another component which displays the welcome message. Message is displayed like this

export class MainPageComponent implements OnInit {

  public messages = [];

  constructor(public service: SharedService) {}

  ngOnInit() {
    this.DisplayMessage();
  }

  public DisplayMessage() {
    this.service.JoinMessage().subscribe((data) => this.messages.push(data));
  }
}

When I open two tabs and log in, The welcome message is not shared between this two tabs. The first loged in client is not receiving the message that welcomes the second client. I'm assuming the connectionId in chathub should be same for both clients to communicate with eachother however it's different.

CodePudding user response:

you need to return the correct Observable when subscribing (do not use of() here, use Subject.next() instead)

private message$: Subject<any> = new Subject();

 JoinMessage(): Observable<string> {
    this.hubConnection.on("ReceiveMessage", (messages) => {
      this.message$.next(messages);
    });
    return this.message$.asObservable();
  }
  • Related