AJAX + Signal R problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

AJAX + Signal R problem

Im trying to do chat between users using ASP NET MVC, ajax and java script. I have functional View which is displaying messages in real time. However, when i try to make it into ajax, it won`t display it in real time (still saves to database) Problem might be caused in connection to hub, because chat.js is loaded only once, with chat id of 1 (default). When chatId is changed with ajax, its correctly displayed in hiden input field, but I think connection is same Link to code: https://www.sololearn.com/compiler-playground/ceK35Aoy9ksG

25th Nov 2023, 7:11 PM
Artur - avatar
1 Answer
+ 1
I think that you are trying to load and display chat messages in real-time using SignalR and AJAX. The issue you are facing might be related to the fact that the chat.js file is loaded only once, and the SignalR connection is established based on the initial chat ID. To resolve this issue, you can modify the chat.js file to dynamically handle different chat IDs. Instead of initializing the SignalR connection outside of the event handlers, you can move it inside the loadChat function, ensuring that the connection is re-established when the chat ID changes.
24th Dec 2023, 8:57 AM
卂ㄚㄩ丂卄 - avatar