C
C#7mo ago
Bordin

SignalR

Hi, I am trying to create a Notification feature for my application and I figured it should be Real-time. Which is why i am trying to use SignalR but i don't quite understand it. I made a notification hub
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;
namespace LebUpwork.Api.Hubs
{
public class NotificationHub : Hub
{
public async Task SendNotification(string user, string message)
{
await Clients.All.SendAsync("ReceiveNotification", user, message);
}
}
}
using Microsoft.AspNetCore.SignalR;
using System.Threading.Tasks;
namespace LebUpwork.Api.Hubs
{
public class NotificationHub : Hub
{
public async Task SendNotification(string user, string message)
{
await Clients.All.SendAsync("ReceiveNotification", user, message);
}
}
}
So, i really dont understand what a hub is and i have read and asked chatgpt but i can't figure it out. What's a user? what's a message? how does it know who the client is? i really dont understand anything I added the hub in program.cs btw so that step is done. I just dont know how that is supposed to send notifications because apparently it shouldn't return anything.
3 Replies
Bordin
Bordin7mo ago
const [conn, setConn] = useState<HubConnection | null>(null);

const userId: number = 5;

useEffect(() => {
const startConnection = async () => {
try {
const connection = new HubConnectionBuilder()
.withUrl("https://localhost:7170/Hubs/NotificationHub")
.build();

connection.on("ReceiveNotification", (notification) => {
console.log("Received notification:", notification);
//i suppose receivednotification is like a trigger to post getapi? but that sitll doesnt make sense. because u dont know the exact thing u want to get and ofcourse u wont get everything even the data you received before.
});

await connection.start();
console.log("SignalR Connected");
setConn(connection);
} catch (err) {
console.error("SignalR Connection Error:", err);
}
};

startConnection();


return () => {
if (conn) {
conn.stop();
}
};
const [conn, setConn] = useState<HubConnection | null>(null);

const userId: number = 5;

useEffect(() => {
const startConnection = async () => {
try {
const connection = new HubConnectionBuilder()
.withUrl("https://localhost:7170/Hubs/NotificationHub")
.build();

connection.on("ReceiveNotification", (notification) => {
console.log("Received notification:", notification);
//i suppose receivednotification is like a trigger to post getapi? but that sitll doesnt make sense. because u dont know the exact thing u want to get and ofcourse u wont get everything even the data you received before.
});

await connection.start();
console.log("SignalR Connected");
setConn(connection);
} catch (err) {
console.error("SignalR Connection Error:", err);
}
};

startConnection();


return () => {
if (conn) {
conn.stop();
}
};
also connection is failing, it's giving errors on frontend Negotiation errors
Insire
Insire7mo ago
a hub is just an endpoint that your clients connect to. connections are stateful and permanent, until the client disconnects. so the server knows about all connections and connected clients. you can send anything across a connection, as that will be serialized and if you have a listener on the other side, then it will be received so it doesnt matter what a user or a message is, because you design that object they are, what ever you say they are idk, about your client side error about Negotiation errors, if you dont configure stuff properly, then things are going to fail. if you are using authentication, then you need to use that for your signalr connection aswell
Bordin
Bordin7mo ago
Ah yes im using authentication. It's probably that!
Want results from more Discord servers?
Add your server