C
C#2w ago
MR.ZeRo

✅ Help,web chat, i’m stuck

Hi, so i want to create a simple web chat(using mvc) , and i did, i wanted to make more progress and create users like login and stuff, btw i put sqlite as database so when i want to save the messages into the database i have to change this
public class ChatHub : Hub
{
private readonly ChatContext _context;
public ChatHub(ChatContext context)
{
_context = context;
}
public async Task SendMessage(string user, string message)
{
var messageEntity = new Message
{
UserId = 1, // Replace with actual user ID
Content = message,
Timestamp = DateTime.UtcNow
};
_context.Messages.Add(messageEntity);
await _context.SaveChangesAsync();

await Clients.All.SendAsync("ReceiveMessage", user, message);

}
public class ChatHub : Hub
{
private readonly ChatContext _context;
public ChatHub(ChatContext context)
{
_context = context;
}
public async Task SendMessage(string user, string message)
{
var messageEntity = new Message
{
UserId = 1, // Replace with actual user ID
Content = message,
Timestamp = DateTime.UtcNow
};
_context.Messages.Add(messageEntity);
await _context.SaveChangesAsync();

await Clients.All.SendAsync("ReceiveMessage", user, message);

}
And create context and stuff but when i run the application and want to send a message the message won’t send and gives me error in browser this:.error(err.toString());}); In my chat.js Anyway here’s my git: https://github.com/Spaceismyhome/SignalRchatAppMCV
GitHub
GitHub - Spaceismyhome/SignalRchatAppMCV: this is from the Microsof...
this is from the Microsoft stuff, but it's under work, so i can make it a proper chat app, and for actual apps - Spaceismyhome/SignalRchatAppMCV
112 Replies
Angius
Angius2w ago
Add "anything" like... what There's no way that adding var i = 2 + 2 would break anything, for example
Harbour
Harbour2w ago
Check connection strings, make sure these are correct. Are you awaiting the send message method also Would be useful uploading the repo so folk can check more thoroughly
MR.ZeRo
MR.ZeRoOP2w ago
Like private readonly AppDbContext _context; public ChatHub(AppDbContext context) { _context = context; } public async Task SendMessage(string user, string message) {
_context.Messages.Add(new ChatMessage { User = user, Message = message, Timestamp = DateTime.UtcNow }); await _context.SaveChangesAsync();
await Clients.All.SendAsync("ReceiveMessage", user, message); } But I can’t send the message when i click send after I debug the app? (When it works) I didn’t upload it to my git yet but when i do i will edit my post thx for the note.
Angius
Angius2w ago
$code
MODiX
MODiX2w ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Angius
Angius2w ago
And what exactly happens when you add this bit of code?
MR.ZeRo
MR.ZeRoOP2w ago
I debug the the app so that i go to https localhost to try if i can send messages, but when i put messages in the box with the id, and i click the button send it won’t send it
Angius
Angius2w ago
Does the message get added to the database? Also, what is Clients.All.SendAsync()?
MR.ZeRo
MR.ZeRoOP2w ago
So that you can send the message to all clients (users) in one big room
Angius
Angius2w ago
Right, yeah, but what does it use, how does it work?
MR.ZeRo
MR.ZeRoOP2w ago
I just try sqlite so I don’t know how to check it tbh
Angius
Angius2w ago
Is it a static class that holds some websocket connection? A SignalR client you're keeping in a property?
MR.ZeRo
MR.ZeRoOP2w ago
SignalR
Angius
Angius2w ago
Did you follow the docs? https://learn.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-9.0&tabs=visual-studio#create-a-signalr-hub Seems your SendMessage() method should be in a SignalR hub
MR.ZeRo
MR.ZeRoOP2w ago
Yes.
Angius
Angius2w ago
Step through the code and see if at least await Clients.All.SendAsync("ReceiveMessage", user, message); gets invoked
MR.ZeRo
MR.ZeRoOP2w ago
It’s invoked in in wwwroot in js chat.js Sorry my code is messy i was searching for it
"use strict";

var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();

//Disable the send button until connection is established.
document.getElementById("sendButton").disabled = true;

connection.on("ReceiveMessage", function (user, message) {
var li = document.createElement("li");
document.getElementById("messagesList").appendChild(li);
// We can assign user-supplied strings to an element's textContent because it
// is not interpreted as markup. If you're assigning in any other way, you
// should be aware of possible script injection concerns.
li.textContent = `${user}: ${message}`;
});

connection.start().then(function () {
document.getElementById("sendButton").disabled = false;
}).catch(function (err) {
return console.error(err.toString());
});

document.getElementById("sendButton").addEventListener("click", function (event) {
var user = document.getElementById("userInput").value;
var message = document.getElementById("messageInput").value;
connection.invoke("SendMessage", user, message).catch(function (err) {
return console.error(err.toString());
});
event.preventDefault();
});
"use strict";

var connection = new signalR.HubConnectionBuilder().withUrl("/chatHub").build();

//Disable the send button until connection is established.
document.getElementById("sendButton").disabled = true;

connection.on("ReceiveMessage", function (user, message) {
var li = document.createElement("li");
document.getElementById("messagesList").appendChild(li);
// We can assign user-supplied strings to an element's textContent because it
// is not interpreted as markup. If you're assigning in any other way, you
// should be aware of possible script injection concerns.
li.textContent = `${user}: ${message}`;
});

connection.start().then(function () {
document.getElementById("sendButton").disabled = false;
}).catch(function (err) {
return console.error(err.toString());
});

document.getElementById("sendButton").addEventListener("click", function (event) {
var user = document.getElementById("userInput").value;
var message = document.getElementById("messageInput").value;
connection.invoke("SendMessage", user, message).catch(function (err) {
return console.error(err.toString());
});
event.preventDefault();
});
Angius
Angius2w ago
Step through the C# code and see if the forementioned method call is hit, or if maybe the database saving throws an exception or whatever
MR.ZeRo
MR.ZeRoOP2w ago
What do you mean? Btw the database is sqlite, and I don’t have so many experience with it, i just used EF CORE sqlite and connect a controller with it so that it can make a database, but I don’t know how to check it if the data is saved
Angius
Angius2w ago
You can make a controller that will query this data, for example. Or use the SQLite CLI to query the database file As for what I mean, I mean create a breakpoint inside of this method, run the code in debug mode, step through the code line by line, and see if you maybe get an exception somewhere or something From what you're saying, the method is behaving as if await Clients.All.SendAsync("ReceiveMessage", user, message); is never called Well, let's check if it is actually called
MR.ZeRo
MR.ZeRoOP2w ago
I will check and i will get back later thanks for your help man, appreciate it, i got some docs too to lock at so to know where I did wrong, i have to sleep right now, but if i got stuck i will send a message, thanks again.
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
Hi, so did inspect the browser and it gives me this
No description
MR.ZeRo
MR.ZeRoOP2w ago
And apparently it doesn’t save anything in the DB, I’ve downloaded DB browser (sqlite) app to check it
Angius
Angius2w ago
console.error() returns a void, you shouldn't return it In any case, what does it print to the console?
MR.ZeRo
MR.ZeRoOP2w ago
How so? You mean after debug? It won’t send the message
Angius
Angius2w ago
No description
Angius
Angius2w ago
console.error() prints something to the browser console What does it print to console?
MR.ZeRo
MR.ZeRoOP2w ago
There’s no console, I don’t really know, i mean, what console? Sorry but I don’t know really I don’t where to find that?
Angius
Angius2w ago
No description
MR.ZeRo
MR.ZeRoOP2w ago
No description
Angius
Angius2w ago
Right, so the error happened somewhere in this SendMessage() method
MR.ZeRo
MR.ZeRoOP2w ago
No description
Angius
Angius2w ago
Check the server logs and see if they mention any exceptions
MR.ZeRo
MR.ZeRoOP2w ago
ive only found this tbh
No description
MR.ZeRo
MR.ZeRoOP2w ago
GitHub
GitHub - Spaceismyhome/SignalRchatAppMCV: this is from the Microsof...
this is from the Microsoft stuff, but it's under work, so i can make it a proper chat app, and for actual apps - Spaceismyhome/SignalRchatAppMCV
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
Tbh I don’t know how to do that but thx, also i am really tired with this shit so i’m gonna just delete the whole thing, it’s not like i did a great change i can re-do it in minutes maybe then it will be better
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
You mean i might’ve installed wrong ver of EF?
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
Yea, I figured 😓🤣
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
…👍 tbh most of it flew over my head but when i redo my project i will come here to take a better look, thx for the notes though. I mean I don’t have really a good experience I’ve learn all by myself but I might’ve skipped some stuff I didn’t know
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
No, i was trying to do the database and then use them Like for example when i sent a message to be saved into the database and stuff then take care of the login and chatroom stuff
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
Serious answer:what is that?
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
Honestly i think it was just there
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
I searched any there was mvc they said that good for chat app, and the Microsoft doc use that
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
I had some exp with creating web api but I didn’t think about it that way for creating chat app
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
Yep, i just wanted to create a simple chat app, so that i can be more experienced and went down to the rabbit hole Yea, I’ve learned alot of html and css
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
So what should i use? Man i need it i mean it was for fun project, now it’s my graduation project 😭😭
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
Oh, like 4-5 months But i want to do it quick cuz i do it alone my colleagues don’t know shit and it’s mandatory to have partners
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
That’s it? Html,razor and blazor?
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
Tbh, i heard that signalr is easier, and I don’t have so much experience with servers, i hate that server stuff tbh(and network related)
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
I mean i suck at it, I really don’t know what I’ve been doing at all, so that’s why i was searching
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
Ah, yes, i knew it, that’s why i will just delete it and begin anew its much easier and I didn’t get that far Btw , do i need just pick razor in creating new projects? In the .net? And do the stuff like ef and signalr and db? Instead of mvc or just use web API?
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Angius
Angius2w ago
Razor Pages has been working well for me, well above 5 pages ¯\_(ツ)_/¯
MR.ZeRo
MR.ZeRoOP2w ago
Well, i need to create chat groups and private one to one chats too, so api it is?
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
I have no idea about razor so i need to watch a tutorial to learn the basics tbh
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
Need to learn that too😭
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
Can’t i use html and css for it?
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
It’s easy for html and css for me at least, but i need functionality right now instead of ui so I would make bare minimum and when the app works well then i will consider make it beautiful
Angius
Angius2w ago
The JS code in the repo uses function (){} for lambdas, I'd say the answer is no
MR.ZeRo
MR.ZeRoOP2w ago
Not really as i said i’m totally beginner if you like to say just watch my git it’s all my projects
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
I don’t know what to say at this point 😂😓
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
As i said just do the project all over again, it’s much easier , so i use web api, and bare html and then razor that’s it?
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
?
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Angius
Angius2w ago
Most of it was Bootstrap, from what I've seen lmao
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
? Atp I don’t even know what you guys are talking about.
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
Yea i just found out, appreciate it, but the reason why I asked was cuz i want to do it myself, if someone did fix the problem for me I wouldn’t learn.
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Angius
Angius2w ago
Right, so we still didn't really figure out what the issue was?
MR.ZeRo
MR.ZeRoOP2w ago
😂😂😂😂😂, he did, it’s up there he fixed it, but I don’t get it.
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
Angius
Angius2w ago
Ah
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX2w ago
TeBeCo
Quoted by
<@689473681302224947> from #Help,web chat, i’m stuck (click here)
From TeBeCo
React with ❌ to remove this embed.
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
🤷😓, don’t make your self more tired man, i will just re do the project with web API and stuff i will figure it out.also I merged the repo request in git you sent thx btw
Angius
Angius2w ago
I wouldn't be using a plain API project for this tbh, if for the ease of use alone It's easier to have an Index.cshtml razor page or whatever where you can dump your HTML, and an API on the side Than trying to return html from a bare API
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
Yea, i meant, web api,html, razor
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
Yea, it’s as i had in my mind, like i’m not that stupid to try to put all the code in one place😂
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
I might, and didn’t notice but thanks that helped alot tbh
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View
MR.ZeRo
MR.ZeRoOP2w ago
No, problem, i’m not home now that’s why, and I don’t use discord on my laptop a lot tbh😂
Unknown User
Unknown User2w ago
Message Not Public
Sign In & Join Server To View

Did you find this page helpful?