Ask Learn
Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
A new twist to a boring Chat application
Most demos on Node.js illustrate how to create a chat server, but rarely provide the client side of the chat service.
That is what this post is about – creating both the server and client side of the equation.
Server | Node.js | Javascript |
Client | .NET | C# |
Node.js – Chat Application – Server Side
This is your traditional Node.js chat application. I’ve commented it pretty heavily. Note the following:
Create a TCP socket listener | var s = net.Server(function(socket).... |
Saving the socket connection so we can send message back to the client | sockets.push(socket).... |
An event that represents a client sending a text msg to server. 'data' represents the event and msg_sent is the data sent by chat client application. | socket.on('data', function(msg_sent) {.... |
Don't echo back the message to the sender | if(sockets[i] == socket) continue; |
Write message back to listeners (except original sender) | sockets[i].write(msg_sent); |
An event when a client disconnects. We need to remove dead sockets from our array. | socket.on('end', function() { |
Listening on https://localhost:8000 | s.listen(8000); |
Filename = lamechat.js
|
Creating Chat Applications in .NET and C#
This next section will demonstrate the building of a chat client for the Node.js server.
Create a new Visual Studio Project
Project type = Windows Presentation Foundation Application
Editing MainWindow.xaml
Right mouse click on MainWindow.xaml
Where to add the various controls
We are adding textboxes, command buttons.
The graphical interface – MainWindow.xaml
MainWindow.xaml is where we define our user interface. We have 3 text boxes and 2 buttons.
txtChatName | Allows you to login with an id |
txtConversation | Where the conversation between chat clients can be seen |
txtOutMsg | The text message a given chat client wants to send to others |
cmdConnect | Allows you to login and connect using txtChatName |
cmdSendMessage | Sends your text message that was typed into txtOutMsg |
Filename = MainWindow.xaml
|
What the finished interface looks like
All the controls have been added.
The code behind – C# – chat client application logic
The next section is about writing the client code that connects, sends, and receives text/chat messages.
Proper Using Statements
“using” statements are needed to minimize typing in large amounts of code.
|
The final code in MainWindow.xaml.cs
This represents all the code needed to complete the C#-based client application.
Adding the code behind.
MainWindow.xaml.cs
Here is the entire code-behind module. No extra references are needed.
|
Let’s build the project
Right mouse click to see the “rebuild” command.
Should be a clean “recompile”
“0” errors.
Testing NodeJsChat
It is time to test the applications.
Start by running lamechat.js
Done at a command prompt.
Run the executables
Start by opening the project folder and navigating to the “bin\debug” folder.
We need at least two client chat applications to test
You will simply run NodeJsChatClient.exe twice.
The final test
Login to both and then start sending messages.
This represents the final step
Now just improve what I did and send it back to me
Anonymous
May 19, 2014
Hi Bruno,
your tutorial is amazing :)
I have one question about the client side: what is the purpose of that Thread.Sleep(500) at the end of taskOpenEndpoint?
Thank you in advance.
Anonymous
June 15, 2014
The comment has been removed
Anonymous
July 02, 2014
Hello BrunoTerkaly,
your tutorial is helpful, I really like it.
I am a newbie in node.js.
your tutorial can chat between 2 .net app but I try make a application to chat between web and .net app, but I don't know how to do website part.
could you please help me
Anonymous
July 02, 2014
Hello BrunoTerkaly,
I mean that I want to make Chat Applications in Html and javascript instead of in .NET and still using node.js server
Anonymous
May 05, 2015
dude you are amazing!
thanks
lehelmatyus.com
Anonymous
November 08, 2015
Thank you for this post,
In fact I have a question regarding this implementation, in the client side you are using a pooling architecture thanks to the while(true) loop asking the node server for new data. My question is the following is there a way to have a persistent connection between a C# client and a node js server with a websocket protocol?
I was looking at a SocketIO4Net project in git Hub that does not work with the last version of socket.io. I could not find a solutation for it.
Is there any solution proposed? or in progress for future?
Thank you for your answer
Achiou
Please sign in to use this experience.
Sign in