Let's Create a Chat Addin for your Web Site

The chat feature on the web page to provide chat between the visitors, obviously would be good. I will try to explain how you can prepare this type of web page addins via PokeIn. If you have a rush, you can download the sample project from the link above. This time, I created a sample for VB.NET too. Please don't forget, you need to download PokeIn Library and add that as a reference on your project.

I had mentioned about the entry points of PokeIn in "this" article before. And now, I want to talk about the other parts of library.

For every new connection, PokeIn creates an instance of this class with the clientId of connection. So, ChatApp class stores this information and waits for the username. Client side sends the username via SetName function call and if there is no same username in the Users dictionary, client side gets UsernameSet event. After it, Client can call the Send function to send messages to others.

As you can see, we created a manager class for a web application just like for a desktop application.

VB.NET

As you know from the first article, SendToClient function sends messages to browser side to run. So, every message may have a string parameter in own function parameter list. And value of those parameters may contain some harmful characters for our JavaScript function body. To discard these types of errors easily, we can use this method.

If the user closes the chat window, we need to remove him safely from our active user list to free resources and available names. So, when PokeIn realizes that the user is offline, it will remove the object instances the client has. In this sample, our class destructors will run and we can remove the user information from our lists.

What for Lock / SyncLock ?

"Users" and "Names" dictionaries are usable for all active users concurrently (don't forget! we have created a multiuser application). So, for thread safety, we locked the shared objects. So we know that the shared dictionaries will be accessible by the entire application safely. If you downloaded the sample application, probably you saw that I locked these shared objects every time I used them.

Tricks

PokeIn.Comet.BrowserHelper.RedirectPage: Allows you to redirect given client to given URL (useful for kick? ), i.e., You can track the chat activity of each client and kick some of them via remoting their page to your application home, etc. Or some server operations may need to refresh all client pages.

PokeIn.Comet.BrowserHelper.SetElementProperty: You can set any property of any client side object from the server side.There will be some client elements that need changes directly from server side code.

PokeIn.Comet.BrowserHelper.SetElementEvent: You can listen to client side element events from the server side (very useful?)You can prepare the autocomplete controls easily with this method.