Difference: 1. nodejs is a JS running environment, while WebSocket is a communication protocol; 2. nodejs is used to easily build network applications with fast response and easy expansion, while WebSocket can make the client Data exchange with the server becomes simpler, allowing the server to actively push data to the client.
The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, DELL G3 computer.
What is nodejs
Node is a JavaScript running environment based on the Chrome V8 engine, a development platform that allows JavaScript to run on the server. Used to easily build network applications with fast response and easy expansion. Node uses an event-driven, non-blocking I/O model to be lightweight and efficient, making it ideal for running data-intensive real-time applications on distributed devices.
Node uses an architecture called an "event loop", making it easy and safe to write highly scalable servers. There are a variety of techniques for improving server performance. Node has chosen an architecture that can both improve performance and reduce development complexity. This is a very important feature. Concurrent programming is often complex and full of landmines. Node bypasses these but still provides great performance.
Node uses a series of "non-blocking" libraries to support the event loop. Essentially, it provides interfaces for resources such as file systems and databases. When sending a request to the file system, there is no need to wait for the hard disk (to address and retrieve the file). The non-blocking interface will notify Node when the hard disk is ready. This model simplifies access to slow resources in a scalable way that is intuitive and easy to understand. Especially for users who are familiar with DOM events such as onmouseover and onclick, they will feel familiar.
Although letting Javascript run on the server side is not a unique feature of Node, it is one of its powerful features. We have to admit that the browser environment limits our freedom to choose programming languages. Any desire to share code between servers and increasingly complex browser client applications can only be achieved through Javascript. Although there are other platforms that support Javascript running on the server side, Node has developed rapidly and become the de facto platform because of the above characteristics.
15 Nodejs application scenarios
We already have a preliminary understanding of Nodejs, let’s take a look at the application scenarios of Nodejs.
1 Web development: Express EJS Mongoose/MySQL
express is a lightweight and flexible Nodejs web application framework that can quickly build a website. The Express framework is built on the built-in Http module of Nodejs and repackages the Http module to implement the function of Web request processing.
ejs is an embedded Javascript template engine that generates HTML code through compilation.
mongoose is the object model tool of MongoDB. Through the Mongoose framework, you can access MongoDB.
mysql is a communication API that connects to the MySQL database and can be used to access MySQL.
Normally using Nodejs for web development requires the use of three frameworks, just like SSH in Java.
2 REST development: Restify
restify is a REST application framework based on Nodejs, supporting server and client. Restify focuses more on REST services than express, removing the template, render and other functions in express. At the same time, it strengthens the use of REST protocol, versioning support, and HTTP exception handling.
3 Web Chat Room (IM): Express http://Socket.io
socket.io is a software based on the Nodejs architecture and supports the websocket protocol for real-time communication. Bag. Socket.io provides a complete package for building real-time applications across browsers. Socket.io is completely implemented by JavaScript.
4 Web crawler: Cheerio/Request
cheerio is a fast, flexible and encapsulating jQuery core function toolkit specially customized for the server. Cheerio includes a subset of core jQuery, stripping away all DOM inconsistencies and browser incompatibilities from the jQuery library, revealing its truly elegant API. Cheerio works on a very simple and consistent DOM model, making parsing, operation, and rendering incredibly efficient. Basic end-to-end benchmarks show that Cheerio is approximately eight times (8x) faster than JSDOM. Cheerio encapsulates the @FB55 compatible htmlparser and can parse almost any HTML and XML document.
5 Web blog: Hexo
Hexo is a simple, lightweight, static blog framework based on Node. Through Hexo, we can quickly create our own blog, which can be completed with only a few commands.
When released, Hexo can be deployed on your own Node server or on github. For individual users, deploying on GitHub has many benefits. It can not only save the cost of servers, but also reduce the troubles of various system operation and maintenance (system management, backup, network). Therefore, personal sites based on github are beginning to become popular...
6 Web Forum: nodeclub
Node Club is a new community software developed with Node.js and MongoDB, with an elegant interface and powerful functions Rich, compact and fast, it has been used in the Node.js Chinese technical community CNode, but you can use it to build your own community.
7 Web Slideshow: Cleaver
Cleaver can generate presentations based on Markdown. If you already have a Markdown document, you can create a slideshow in 30 seconds. Cleaver is a tool for Hackers.
8 Front-end package management platform: bower.js
Bower is a package management tool launched by twitter. Based on the modular idea of nodejs, the functions are dispersed into various modules, so that the modules and There is a relationship between modules, and Bower is used to manage this relationship between modules.
9 OAuth authentication: Passport
The Passport project is an authentication middleware based on Nodejs. The purpose of Passport is only for "login authentication". Therefore, the code is clean, easy to maintain, and can be easily integrated into other applications. Web applications generally have two forms of login authentication: username and password authentication login, and OAuth authentication login. Passport can configure different authentication mechanisms according to the characteristics of the application. This article will introduce the authentication login of user name and password.
10 Scheduled task tool: later
Later is a tool library based on Nodejs that executes scheduled tasks in the simplest way. Later can run in Node and the browser.
11 Browser environment tools: browserify
The emergence of Browserify allows Nodejs modules to run in the browser, use the require() syntax format to organize the front-end code, and load npm modules. In the browser, the code compiled by calling browserify is also written in the <script> tag. </script>
The operation of using Browserify is divided into 3 steps. 1. Write a node program or module, 2. Use Browserify to precompile it into bundle.js, 3. Load bundle.js in the HTML page.
12 Command line programming tool: Commander
commander is a lightweight nodejs module that provides powerful functions for user command line input and parameter parsing. Commander originates from a Ruby project of the same name. Features of commander: self-recording code, automatically generating help, merging short parameters ("ABC" == "-A-B-C"), default options, mandatory options, command parsing, and prompts.
13 Web console tool: tty.js
tty.js is a command line window that supports running in the browser. It is based on the node.js platform and relies on the socket.io library. Through websocket communicates with Linux system. Features: supports multi-tab window model; supports vim, mc, irssi, vifm syntax; supports xterm mouse events; supports 265-color display; supports session.
14 Client application tool: node-webwit
Node-Webkit is a fusion of NodeJS and WebKit technologies, providing an underlying framework for client application development across Windows and Linux platforms, leveraging popular A platform for writing applications using web technologies (Node.JS, JavaScript, HTML5). Application developers can easily leverage Web technologies to implement various applications. Node-Webkit's performance and features have made it the world's leading web technology application platform.
15 Operating system: node-os
NodeOS is a friendly operating system developed using NodeJS. The operating system is completely built on the Linux kernel and uses shell and NPM For package management, NodeJS can not only perform package management well, but also manage scripts, interfaces, etc. well. Currently, both Docker and Vagrant are built with the first version of NodeOS.
What is websocket
WebSocket is a protocol for full-duplex communication over a single TCP connection. The WebSocket communication protocol was designated as standard RFC 6455 by the IETF in 2011, and was supplemented by RFC7936. WebSocket API has also been designated as a standard by W3C.
WebSocket makes data exchange between the client and the server simpler, allowing the server to actively push data to the client. In the WebSocket API, the browser and the server only need to complete a handshake, and a persistent connection can be created directly between the two for bidirectional data transmission.
What can WebSocket do?
1) Notification function:
Maintain a long connection. When the server sends new messages, it can be pushed to the user in real time. Like notifications, comments, etc. on Zhihu can all use WebSocket communication.
Some clients using H5, in order to simplify development, will also use WebSocket for message notification. Since it is pushed in real time, it will have a better user experience.
2) Data collection:
Some sub-optimal data, such as behavior logs, traces, exception execution stack collection, etc., can open special WebSocket channels for transmission. This can increase the concentration of information and push appropriate configurations based on user behavior in a timely manner. It will make the client-side APM programming model simple since it is supported by most browser kernels.
3) Encryption && Authentication:
Although many WebSocket packages can be captured using Fiddler, Charles, etc. However, if SSL is turned on at the same time and encrypted binary data is transmitted, the cost of cracking will be greatly increased and it will be much safer.
4) Reverse control hook:
This... Because it is a duplex long connection, the server can push some hook commands, or even code directly, for execution on the client. For example, take a screenshot, record a voice, or breed a pony. As long as the user passes the authorization application, the rest is up to you.
The difference between nodejs and websocket
##Difference 1:
Node is a JavaScript running environment based on the Chrome V8 engine, a development platform that allows JavaScript to run on the server.
WebSocket is a protocol for full-duplex communication over a single TCP connection.
Difference 2:
Node is used to easily build network applications that respond quickly and are easy to expand.
WebSocket makes data exchange between the client and the server simpler, allowing the server to actively push data to the client.
For more node-related knowledge, please visit: nodejs tutorial! !
The above is the detailed content of What is the difference between nodejs and websocket. For more information, please follow other related articles on the PHP Chinese website!

HTML and React can be seamlessly integrated through JSX to build an efficient user interface. 1) Embed HTML elements using JSX, 2) Optimize rendering performance using virtual DOM, 3) Manage and render HTML structures through componentization. This integration method is not only intuitive, but also improves application performance.

React efficiently renders data through state and props, and handles user events through the synthesis event system. 1) Use useState to manage state, such as the counter example. 2) Event processing is implemented by adding functions in JSX, such as button clicks. 3) The key attribute is required to render the list, such as the TodoList component. 4) For form processing, useState and e.preventDefault(), such as Form components.

React interacts with the server through HTTP requests to obtain, send, update and delete data. 1) User operation triggers events, 2) Initiate HTTP requests, 3) Process server responses, 4) Update component status and re-render.

React is a JavaScript library for building user interfaces that improves efficiency through component development and virtual DOM. 1. Components and JSX: Use JSX syntax to define components to enhance code intuitiveness and quality. 2. Virtual DOM and Rendering: Optimize rendering performance through virtual DOM and diff algorithms. 3. State management and Hooks: Hooks such as useState and useEffect simplify state management and side effects handling. 4. Example of usage: From basic forms to advanced global state management, use the ContextAPI. 5. Common errors and debugging: Avoid improper state management and component update problems, and use ReactDevTools to debug. 6. Performance optimization and optimality

Reactisafrontendlibrary,focusedonbuildinguserinterfaces.ItmanagesUIstateandupdatesefficientlyusingavirtualDOM,andinteractswithbackendservicesviaAPIsfordatahandling,butdoesnotprocessorstoredataitself.

React can be embedded in HTML to enhance or completely rewrite traditional HTML pages. 1) The basic steps to using React include adding a root div in HTML and rendering the React component via ReactDOM.render(). 2) More advanced applications include using useState to manage state and implement complex UI interactions such as counters and to-do lists. 3) Optimization and best practices include code segmentation, lazy loading and using React.memo and useMemo to improve performance. Through these methods, developers can leverage the power of React to build dynamic and responsive user interfaces.

React is a JavaScript library for building modern front-end applications. 1. It uses componentized and virtual DOM to optimize performance. 2. Components use JSX to define, state and attributes to manage data. 3. Hooks simplify life cycle management. 4. Use ContextAPI to manage global status. 5. Common errors require debugging status updates and life cycles. 6. Optimization techniques include Memoization, code splitting and virtual scrolling.

React's future will focus on the ultimate in component development, performance optimization and deep integration with other technology stacks. 1) React will further simplify the creation and management of components and promote the ultimate in component development. 2) Performance optimization will become the focus, especially in large applications. 3) React will be deeply integrated with technologies such as GraphQL and TypeScript to improve the development experience.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version
God-level code editing software (SublimeText3)

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Atom editor mac version download
The most popular open source editor