Nodejs multi-room web chat room

Source: Internet
Author: User
Tags emit

A year ago to do a small project, after a long time, turned out the PPT document at the moment to summarize. SOURCE Download: Https://github.com/CreekLou/chatRoom

Nodejs Background Introduction

1, JavaScript is first run in the browser, but the browser simply provides a context
2, node. js is actually another context that allows JavaScript code to run in the backend (out-of-browser environment)
3, node. JS is actually both a runtime environment and a library

Nodejs architecture such as


Node. JS's asynchronous mechanism is event-based and all disk I/O, network traffic, database queries are requested in a non-blocking manner, and the returned results are handled by the event loop

? Event-driven callbacks (event polling)? Asynchronous IO avoids frequent context switching? in node except for the code, everything is executed in parallel.

Multi-threaded synchronous I/O with single-threaded asynchronous I/O

Synchronous I/O (blocking):
Using multithreading to deliver throughput
Leveraging multi-core CPUs through event slice segmentation and thread scheduling
Requires multi-core CPU to be used by the operating system to schedule multithreading
Hard to make full use of CPU resources
Large memory track, weak data locality
Line-up programming thinking

asynchronous I/O (non-blocking):
Single thread for high throughput
Leveraging multi-core CPUs through functional partitioning
A single process can be bound to a single-core CPU
Can take full advantage of CPU resources
Small memory track, strong data locality
does not conform to traditional programming thinking


The node. JS program starts with the event loop, ends with the event loop, all the logic is the callback function of the event, so node. js is always in the event loop, and the program entry is the callback function for the first event in the event loop.


Nodejs Core Module 1, the Core module is the heart of node. JS, which consists of a few streamlined and efficient libraries that provide the basic API for node. js
2,process: An object that describes the state of the current node. JS process and provides a simple interface to the operating system, typically used when you write local command-line programs.
3,console: Used to provide console standard output. (IE)
4,util: is a node. JS Core module that provides a collection of commonly used functions
5,events: Is the most important module of node. js, no "one"
6,FS: File system, provides file read, write, rename, delete, traverse directory, link and other POSIX file system operation

module Httpserver, Nodejs server with V8 virtual machine

var http = require ("http");//request (require) node. js's own HTTP module and assign it to the HTTP variable http.createserver (function (Request, response {      //Invoke the function provided by the HTTP module: Createserver  response.writehead ($, {"Content-type": "Text/plain"});  Response.Write ("Hello World");  Response.End ();}). Listen (8888);


Socket.io

Socket.io provides three default events: Connect, message, disconnect.

The Connect event is automatically triggered when a connection is made to the other party, triggering a message event (usually a socket.send () trigger) after receiving data from the other, triggering the disconnect event when the other person closes the connection.
Socket.emit (): Broadcast to the client that established the connection
Socket.broadcast.emit (): broadcasts to all clients that are removing the client that established the connection
Io.sockets.emit (): Broadcast to all clients, equivalent to the two above and

CLIENT (INDEX. HTML)

<script src= "/socket.io/socket.io.js" ></script> <script>varsocket=io.connect (' http://localhost '); Socket.on (' News ', function (data) {      console.log (data);       Socket.emit (' My other event ', {my: ' Data '}); }); </script>
SERVER (APP. Msn

var app = require (' Express ') (), Server = require (' http '). Createserver (APP), Io = require (' Socket.io '). Listen (server); Bind the Socket.io to the server Server.listen (80); App.get ('/', function (req, res) {     res.sendfile (__dirname + '/index.html ');}); /server listens to all clients and returns the new Connection object Io.sockets.on (' Connection ', function (socket) {socket.emit (' news ', {hello: ' world '}); Socket.on (' My other event ', function (data) {Console.log (data);}); });

Chat room function diagram


Reference documents:

http://socket.io/

http://nodejs.org/documentation/tutorials/

Nodejs multi-room web chat room

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.