Simple Chat System

In my most recent project, I implimented a chat system so that registered users could send messages to each other. This is not using SignalR, but works quite well if you are interested in an alternate way to engage registered users within your application.

CodyHouse has a similar example with source code that will get a developer started with the pop out panel. I'll display the tables where messages can be saved in the database. No encryption in my chat system, but could implimented with a quick web search for C# AES 256 bits encryption. There are plenty of examples on GitHub and the CodeProject website.

The database design is very simple and only two tables: PrivateMessage and MembershipUser. There is only one stylesheet and one javascript file. You will need jQuery installed in your project.

Within the application, a user that is logged into the application can search for a member and directly send a message if the administrator has the chat system enabled.

Click on a user's name to view their profile. To send a message, click on the "Message Me" button.

Here is where it becomes neat. From the right side, the chat system is invoked (which is only a couple of divs and javascript). The chat system also has a built in editor which allows font styles and images. I gave the panel a z-index: 9999 so that it will overlay all layers currently rendered.

Let's send some messages and then I'll walk through the code. In the previous message, I was logged in as John_Wick. I'll logout with John_Wick and login as joshyates. After logging into the application as joshyates, I have a notification on my user menu.

I inserted an image and manually adjusted the width and height to fit accordingly with the content. Initally, it will display by the default image size.

The chat session keeps track of the other user has read the message, delivery confirmation, time stamp, and delete functionality.

I'll provide a snapshot of the code and you can download the rest. My plans are to create a small sample project with bare minimum functionality and then I'll upload to GitHub for an open source project. The PrivateMessageController used dependancy injection and the constructor has all services needed for the chat system logic.

Create, Read, and Delete functions.

The Service Layer will access the domain layer with the following methods.

The entity class represents what the PrivateMessage table in the database.

This would be a great module to add within an application not using SignalR (which seems to be the most popular). Overall, it was a great learning experience for me and well worth the time I spent adding this functionality to my most recent project.

Please fill free to leave comments below:

Software Solutions

We want to get involved