Book Image

Learning Node.js for .NET Developers

Book Image

Learning Node.js for .NET Developers

Overview of this book

Node.js is an open source, cross-platform runtime environment that allows you to use JavaScript to develop server-side web applications. This short guide will help you develop applications using JavaScript and Node.js, leverage your existing programming skills from .NET or Java, and make the most of these other platforms through understanding the Node.js programming model. You will learn how to build web applications and APIs in Node, discover packages in the Node.js ecosystem, test and deploy your Node.js code, and more. Finally, you will discover how to integrate Node.js and .NET code.
Table of Contents (21 chapters)
Learning Node.js for .NET Developers
Credits
About the Author
About the Reviewer
www.PacktPub.com
Preface
Index

Introducing Socket.IO


Socket.IO is a mature and well-established library with excellent cross-browser support. It aims to quickly and reliably establish a bidirectional communication channel in a cross-browser compatible way. It provides a consistent abstraction, based on idiomatic JavaScript events, for real-time communication between the client and the server over this channel. If you have ever used SignalR in .NET, you can think of Socket.IO as the JavaScript equivalent.

Implementing a chat room with Socket.IO

Let's implement a chat lobby for users of our application to talk to one another. First, we need to install Socket.IO:

> npm install --save socket.io

The server-side implementation for this is very simple. We just need to tell Socket.IO that, whenever a user sends a chat message, we want to broadcast this to all connected users as given here src/realtime/chat.js:

'use strict';

module.exports = io => {
    io.on('connection', (socket) => {
       socket.on('chatMessage', (message...