Introduction

In the previous article, we learned how to create a real-time chat application using SignalR and Bootstrap, till now we learned creation of group chat, Creation of Private Chat, Title Bar Notification Alert system, Message Count and Clear Chat History. And now, we are going to learn the most trending feature in Chat Application which is “Emoji” or “Smiley” which makes our application more interactive. And another important and useful feature sending attachments through chat, in file attachment we can send pictures/images, Word, PDF, Excel and text file. And also, we can show/download the pictures/document files.

Emoji is a small digital image or icon used to express an idea or emotion in electronic communication, emoji livens up your text messages with tiny smiley faces. We are going to learn implementation of Emoji in the easiest way. There are a number of Emoji Packages available, but here we are going to integrate “EmojinOne”. In my opinion, this is the best smiley package which is easily available on the web. And also, we are going to implement “SlimScroll” Jquery Plugin which provides interactive content scrollbar.

Integration of Emoji

You can download Emoji Reference files which is available on cdnjs.com. Add CSS files in “Content” directory and JavaScript files in “Scripts” folder. After adding these files in project, add these files in “Chat.aspx” Design Page inside the header tag.

After adding these files, now we have to define Emoji area from where we can add emoji, so here we are defining message textbox as Emoji area. Simply writing the Jquery function as follows:

$(function () {
$("#txtMessage").emojioneArea();
});

Now run your project and you will see the Emoji Box with Message Textbox looks like follows:

We have to apply the same with Private Chat Box; we are creating private chat Box dynamically and embedding this code using Jquery, so we have to define the code dynamically because each time the Id of ChatBox Div will change so on the basis of dynamic id, we have to find the Message Textbox and apply the function:

Again, run the project and the see the output Emoji will apply in private chat box too. Now we have to parse these emojis in our chat, when we received the message which contains Emojis, it get parsed, there are few methods of conversion / parsing of emojis here we are using “.unicodeToImage(str)” method for parsing emoji, it will convert smiley Unicode into image.

Here, we are using online Emoji image resources, if you want to use these resources locally, then you have to download the EmojiOne Smiley Package and change the resource path in “emojionearea.js” file.

If we use these resources locally, it will increase your app performance by decreasing the loading time of images. The output after adding Emoji will look like follows:

Adding Feature of Sending Picture / File Attachment

Send Picture files through the chat, it will add an extra ordinary feature to our app, we can send images, word, document, Text file, PDF File and Excel file. Here, we are adding a separate button for send attachment, we will use Ajax Tool “AsyncFileUpload”. So here, we need to add AjaxToolkit in our project reference, we have already explained adding package in project using Nuget Package Manager in the previous article. So add the package using Nuget Package Manager:

PM> Install-Package AjaxControlToolkit -Version 17.1.1

Now write the code for Attachment button, as we are using AsyncFileUpload for file upload but we are not showing it as we are wrapping File Upload inside the button, so user can only see the button and when it clicks on attachment button, FileUpload events will fired, the design code for attachment button:

There are Three Events generated by the “AsyncFileUpload” while clicking on FileUpload, in which two events are Client Side event and one is Server Side event, “OnUploadedComplete” is the server side event where we are storing files in a particular directory:

First, it will call client side event “OnClientUploadStarted”, as we are using temporary HTML image tag for display and loading the image, while loading the image, we are displaying it for the time being for getting image properties which we are using further while displaying image in chat, so in this function, we are clearing temporary image path:

There are two functions where we are validating upload file by checking the extension of requested file, this function validates only Image and Document files, you can add some more extensions as per your need, code for File Validation:

Here, we are separating Image file and document file as we are going to assign separate design for both, for image file, we are showing image preview in chat, and for document file, we just showing the document icon:

After uploading the image/ document file we displaying in chat, and also showing file properties such as file size image dimension and file type, user can view the full image also can download the image / document.

When we click view button, the image file will open in Modal popup, for this we are using Booststrap Modal, we are passing image path in button value and applying this path to the image inside the modal and then showing the modal.

When the number of messages increase in ChatBox, the default browser vertical scrollbar will apply to the Chatbox, but here we will add something stylish scrollbar so will add Jquery “SlimScroll” in Chatbox. Will add this through the Nuget Package Manager:

We have to apply SlimScroll in both chat boxes in private chat box as well as in group chat box, after applying, you can see the effect of SlimScroll looks like:

Output

Now run the project and you will see that the final output will look like below:

Conclusion

Here, we learned the integration of Emoji in Private Chat and Group Chat with SignalR and sending attachment through chat by using Ajax Toolkit component “AsyncFileUpload”. So we learned the integration of AsyncFileUpload and its concepts such as client side events and server side event. And also getting file properties and displaying uploaded file properties, display image file in Modal popup, Downloading files and also Jquery Plugin “SlimScroll” Integration in Scroll bar. So this is the final article of Chat application.

Hope this will help you and you would like this article, if you think we can add more features in our project, please suggest... if I like it, I will write another article with some more new features. Please don’t forget to download the attached project source code for your reference and to see the complete source. Thank you for reading...

Yes we can show the progressbar,for that we can use jquery to show the file uploading progress.. since i did not used the same in this tutorial.. we can show the loading image while uploading the file.. by adding this simple code :