niico
niico

Reputation: 12729

ASP.NET MVC 5 AJAX - Update chat room when local & remote users post

I need to create a simple chat room in ASP.NET MVC 5.

Creating models, view models & using an AJAX ActionLink I can let one user post new data to a partially updated area of the page easily. I have found plenty of examples of this online.

However, I also need to update the 'chat / message window' when the external user sends a message too. How can I send these events to the client - and how can I update the chat by only adding what the external user said (not refreshing all the text of the chat)?

thx.

Upvotes: 0

Views: 1054

Answers (2)

Steen Tøttrup
Steen Tøttrup

Reputation: 3835

If this is a simple chat room, without any "advanced" features, like private messages etc. then the most light-weight solution would be to use server-side events (SSE).

You can take a look at a stackoverflow article here: "How do server-sent events work with ASP.NET MVC?"

SignalR is a great solution, but to work best, it requires web-sockets, something not all hosting options include. SSE does not require any additional features on the server.

Upvotes: 0

Haitham Shaddad
Haitham Shaddad

Reputation: 4456

You can check for new messages every few seconds but this is not a best approach as it includes unnecessary server hits.

The best approach is to use SignalR which abstracts the way your web server can notify the connected clients, these clients can be a browser, desktop or mobile and this is where SignalR really excels.

This tutorial explains exactly what you need step by step

https://www.asp.net/signalr/overview/getting-started/tutorial-getting-started-with-signalr

Upvotes: 3

Related Questions