Instant messages in MVC application - asp.net-mvc-3

I need your help to solve next issue: I am developing MVC application and want to send instant messages between users. This message will ne standard - it's like notification. I am thinking about two optins:
use XMPP protocot, but I don't need the huge amount of options it could provide, I need just basic functionality I've discribed;
have some static array in MVC application with users invitations were send to and ajax function which will call controller action, check if user is in static list. If yes - make message visible on the page. And plus do page autoupdate. But I am not sure about this becouse array will be called very often.
What do you think, guys? Do you have any ideas? I will be very thankfull for any help!

You may take a look at SignalR.

Also checkout Jabbr which is built on SignalR:
https://github.com/davidfowl/JabbR
This is a chat application written by the author of SignalR David Fowler.

You can set jquery timer to update messages with ajax. And when user writes message , it can be saved in Cache or in database if you want to save it permanently.

Related

Laravel : Web Api, how to find out if a user (application) is online?

I am building a web service for mobile application by Laravel 5.8. I need to get the location of online users, search on some properties and send a notification to them.
The problem is I can't find out if a user is online. My solution was to keep pushing/receiving a message to applications every 10 seconds once they get connected for the first time. Clearly this approach has a huge overload on the server and is not practical.
I would be appreciated for any help.
Thanks in advance.
Sounds like you may make use of Real-time connections such as Pusher
You would need presence channels to see who is online in JS, it works more client to client rather than the Laravel server knowing who is online.
You subscribe users to a channel and then you can see who else is on that channel.
https://laravel.com/docs/5.8/broadcasting#presence-channels
Why not make a column to your user table namely 'is_online' ? So every time the user logged in set this field to true ? and when they are logged out or inactive for a specific of time you will set it to false ?

How to implement a chat application in codeigniter website?

I need to implement a chat window on my codeigniter website,and the chat option is not like facebook or gmail chat.The Live conversation is entirely between the admin and the user who has logged into the site,ie live chat for direct customer service
Thanks in advance.
A quick search on github gives me this:
https://github.com/Runnable/Chat-Example-App-for-Codeigniter-API-on-PHP
https://github.com/vgoodvin/ci-chat
https://github.com/llbbl/codeigniter-chat
You can think of it like a thread in a forum where someone creating a thread and the other someone make replies.
Depending on how you want to customize your apps, the interaction between the OP and those who will make replies will be rapid.
EDIT 1:
Assuming that you understand how MVC works, you can have something like this:
Your DB Structure:
Table User
Table Session
Table Message
A user can be in many Session (chat room), A message can only be posted to one chat session and a user can send many messages.
In your chat page you will have a to display the conversation, a giant text box where your user can write their message and a submit button.
When a user click the submit button, it will then make an HTTP POST to your controller, where your controller will cleanse the data (i.e. $this->form_validation->set_rules();
If the posted data is valid, send it to your model where it then be stored to your database.
Everytime the chat page is loaded what you want to do is:
Get the chat message for the associated session ID.
Again this is an oversimplified example. You can fork the code from the github i mentioned and try to install it on your local machine.
If you want messaging or chat and specially for codeigniter than use this library, Mahana-Messaging-library-for-CodeIgniter. I have used this, and i preferred you if you are learner. It have it's on database which you can manage and it will integrate in your project easily.
Implemented using Zopim chat widget,which is suitable for both CMS and MVC websites. https://www.zopim.com/

Callback methods in ASP.NET Web API

I have an ASP.NET Web API server, that have to communicate with different applications on different platforms. And now I want to create a method that would be something like a callback: client application subscribes to it and waits until server fires a message.
Example:
Many users are waiting until new product will be available in store - they subscribe to this "event". When product arrives in store - every customer receives a message, which have to be handled in some case.
Users send a request "Subscribe"
Server receive a request "Product available!"
Server sends every user a message with product details.
User's application processes the message
I tried to find some information about callbacks or duplex in ASP.NET Web API, but the one advice - it's better to use WCF for this approach.
Solutions
In every client application create something like timer, that every N seconds sends a request "Is product available?" until gets "false". When the response will be true - send a message "Get product details". It's causes a lot of traffic and if there will be many clients with these timers - it would be something bad, isn't it?
Create a small callbacks-server (maybe WCF). But in this case would be a lot of problems with communication between this server and apps on different platforms.
Maybe there are some solution in ASP.NET Web API, that i missed.
If you have some ideas how i can solve this problem, please give me an advice.
Thanks for help.
Looks like you want push notifications from your server - which, in this case, can be achieved by combining SignalR with Web API.
Brad Wilson has a great example on this:
code here - https://github.com/bradwilson/WebstackOfLove
NDC Oslo talk explaining all this - http://vimeo.com/43603472
In short, whenever you add new item to the Web API, you can notify all the connected (subscribed) clients:
public void PostNewItem(ToDoItem item)
{
lock (db)
{
// Add item to the database
db.Add(item);
// Notify the connected clients
Hub.Clients.processItem(item);
}
}
SignalR will invoke the processItem function on the client.
Alternatively, you might want to look into JavaScript SSE and Web API PushStreamContent but that is much more low level and SignalR abstracts a lot of this type of stuff for you so it might be more complicated to deal with.
I blogged about this approach here http://www.strathweb.com/2012/05/native-html5-push-notifications-with-asp-net-web-api-and-knockout-js/

Ajax & ASP Classic Notification Display

I'm doing some updates for an intranet i created few years ago using ASP Classic. What i've been ask to do is to ALERT all users when news on intranet is added. Basically i need to check on database when news is added and than trigger an action to all intranet users saying you have New Message
I'have done a lot of research on this and i could't really find a good example for it.
Your help will be much appreciated.
Cheers
Vullnet
Basically to need to poll the server using AJAX and check for new messages.
I suppose you have a messages table with and ID that counts up. You need to track each user's last message pointer, and send this as part of your AJAX request ad a parameter. If the server's ID is higher thsn the user's then they have a new message.
Based on the AJAX response you then alert the user.
You need to make an ASP that responds to your AJAX call, Pass it a number and it responds with "OK" (not new) or "NEW".
IT works the same way, regardless of the server-side language. Use jQuery to perform your AJAX calls.

ASP.NET MVC: Send email using SendAsync (System.Net.Mail)

is there any way in MVC to get the System.Net.Mail SendAsync to work, instead of the blocking Send method?
I tried using it but hit the "Page starting an asynchronous operation has to have the Async attribute" error, which I obviously can't resolve (or can I?) because there is no ASPX page with an #Page directive where I could add the Async attribute.
Help is greatly appreciated :(
If you're using a third party service such as gmail to send your email and relying on SendEmail or SendAsync you may want to set up your own SMTP server which will then relay onto gmail. The benefit is that SendEmail will return much faster and you may be able to forget the whole async thing.
That way if gmail rejects you for whatever reason the email will still sit in your local queue.
This is a quick and dirty way to get somewhat reliable email sending setup.
Instructions (not personally tested, but i did this years ago*) or see this for Windows 7
*of course I only remembered i did this after spending a long time converting over to async :-(
It looks like you want Asynchronous support for ASP.NET MVC. See also "Extend ASP.NET MVC for Asynchronous Action".
This SO question is also relevant.
You may want to consider putting the email into a database table and then have a daemon that sends out emails by processing the queue.
a) if youre not sending billions of emails you can have a log of what you sent
b) you can resend if something fails.
depends how important these emails are, but if theyre important from a business standpoint and mustnt get lost then you should use this approach.

Resources