Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between nodejs and websocket

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What is the difference between nodejs and websocket, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Differences: 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 to expand, while WebSocket makes the data exchange between the client and the server easier, allowing the server to actively push data to the client.

This tutorial operating environment: windows7 system, nodejs version 12.19.0, DELL G3 computer.

What is nodejs?

Node is a JavaScript running environment based on Chrome V8 engine, a development platform that allows JavaScript to run on the server, which is used to easily build network applications with fast response and easy to expand. Node uses event-driven, non-blocking Istroke O model to make it lightweight and efficient, which is very suitable for running data-intensive real-time applications on distributed devices.

Node uses an architecture called event Loop (event loop), which makes it easy and secure to write highly scalable servers. There are a variety of techniques for improving server performance. Node chose an architecture that not only improves performance but also reduces development complexity. This is a very important feature. Concurrent programming is often complex and landmine-ridden. Node bypasses these, but still provides good performance.

Node uses a series of "non-blocking" libraries to support event loops. In essence, it is to provide 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 (addressing and retrieving files), and the non-blocking interface notifies the Node when the hard disk is ready. The model simplifies the access to slow resources in an extensible way, which is intuitive and easy to understand. Especially for users who are familiar with onmouseover, onclick and other DOM events, there is a sense of deja vu.

While letting Javascript run on the server side is not unique to Node, it is one of its powerful features. Admittedly, the browser environment limits our freedom to choose a programming language. The desire to share code between any server 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 a de facto platform because of the above features.

15 Nodejs application scenarios

Now that we have a preliminary understanding of Nodejs, let's take a look at the application scenario of Nodejs.

1 Web development: Express + EJS + Mongoose/MySQL

Express is a lightweight and flexible Nodejs Web application framework, which can build websites quickly. The Express framework is based on the built-in Http module of Nodejs, and the Http module is repackaged to actually handle Web requests.

Ejs is an embedded Javascript template engine that compiles code to generate HTML.

Mongoose is the object model tool of MongoDB. Through the Mongoose framework, you can access MongoDB.

Mysql is the communication API that connects to the MySQL database and can access the MySQL.

Usually using Nodejs for Web development requires three frameworks to work together, just like SSH in Java.

2 REST development: Restify

Restify is a Nodejs-based REST application framework that supports both the server side and the client side. Restify focuses more on REST services than express, removing template, render and other functions in express, while strengthening the use of REST protocol, versioning support, and exception handling of HTTP.

3 Web chat room (IM): Express + http://Socket.io

Socket.io is a software package that is based on Nodejs architecture and supports websocket protocol for all-time communication. Socket.io provides a complete package for building real-time applications across browsers, and socket.io is implemented entirely by javascript.

4 Web crawler: Cheerio/Request

Cheerio is a fast, flexible, encapsulated jQuery core function toolkit specially tailored for the server. Cheerio includes a subset of the jQuery core, removing all DOM inconsistencies and browser incompatibility from the jQuery library, revealing its truly elegant API. Cheerio works on a very simple, consistent DOM model, parsing, manipulating, and rendering have become incredibly efficient. The basic end-to-end benchmark shows that Cheerio is about eight times faster than JSDOM (8x). Cheerio encapsulates @ FB55 compatible htmlparser and can parse almost any HTML and XML document.

5 Web blog: Hexo

Hexo is a simple, lightweight, Node-based static blogging framework. With Hexo, we can quickly create our own blog, which can be done with just a few commands.

At the time of release, Hexo can be deployed on its own Node server or github. For individual users, deploying on github has many benefits, which can not only save the cost of the server, but also reduce the hassle of various system operation and maintenance (system management, backup, network). So, github-based personal sites are starting to become popular. .

6 Web Forum: nodeclub

Node Club is a new type of community software developed with Node.js and MongoDB, with elegant interface, rich features, compact and rapid. It has been applied in Node.js Chinese technology community CNode, but you can use it to build your own community.

7 Web slides: Cleaver

Cleaver can generate Markdown-based presentations. If you already have a Markdown document, you can make a slide show in 30 seconds. Cleaver is a tool for Hacker.

8 front-end package management platform: bower.js

Bower is a package management tool introduced by twitter. Based on the modular idea of nodejs, it distributes the functions into various modules and makes the relationship between modules and modules, and manages the relationship between modules through Bower.

9 OAuth Certification: Passport

The Passport entry is an authentication middleware based on Nodejs. The purpose of Passport is only for "login authentication", so 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 user name and password authentication login.

10 scheduled task tool: later

Later is a Nodejs-based tool library that performs scheduled tasks in the simplest way. Later can be run in Node and browsers.

11 browser environment tools: browserify

The emergence of Browserify allows the Nodejs module to run in the browser, organize the front-end code with the syntax format of require (), and load the npm module. In the browser, the compiled code that calls browserify is also written in the tag.

The operation with Browserify is divided into three steps. 1. Write node programs or modules, 2. Precompiled into bundle.js with Browserify, 3. Load the 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 is derived from a Ruby project of the same name. Commander features: self-recording code, automatically generate help, merge short parameters ("ABC" = = "- A-B-C"), default options, mandatory options, command parsing, prompt.

13 Web console tool: tty.js

Tty.js is a command line window that supports running in a browser. It is based on the node.js platform, relies on the socket.io library, and communicates with the Linux system through websocket. Features: support multi-tab window model; support vim,mc,irssi,vifm syntax; support xterm mouse events; support 265 color display; support session.

14 client utility: node-webwit

Node-Webkit is the integration of NodeJS and WebKit technology, which provides a low-level framework for client application development across Windows and Linux platforms, and uses the popular Web technology (Node.JS,JavaScript,HTML5) to write applications. Application developers can easily use Web technology to implement a variety of applications. The performance and features of Node- Webkit have made it the world's leading application platform for Web technology.

15 operating system: node-os

NodeOS is a friendly operating system developed by NodeJS, which is completely built on the Linux kernel and uses shell and NPM for package management. NodeJS can not only manage packages, but also manage scripts, interfaces and so on. Currently, both Docker and Vagrant are built using 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 defined as the standard RFC 6455 by IETF in 2011 and supplemented by RFC7936. WebSocket API is also defined as a standard by the W3C.

WebSocket makes it easier to exchange data between the client and the server, allowing the server to actively push data to the client. In WebSocket API, only one handshake is needed between the browser and the server, and a persistent connection can be directly created between the browser and the server for two-way data transmission.

What can WebSocket do?

1) Notification function:

Maintain a long connection, when the service PC game new messages, can be pushed to the user in real time. Like Zhihu's like notification, comments, etc., you can use WebSocket to communicate.

Some clients that use H5 will also use WebSocket for message notification in order to simplify development. Because it is pushed in real time, it will have a better user experience.

2) data collection:

Some sub-optimal data, such as behavior log, trace, exception stack collection, etc., can be transmitted through a special WebSocket channel. This can increase the concentration of information and push the appropriate configuration according to the user's behavior in a timely manner. Since most browser kernels support it, it will simplify the client-side APM programming model.

3) encryption & & Authentication:

Although you can catch a lot of WebSocket packages using Fiddler, Charles, and so on. However, if SSL is enabled at the same time, the transmission of encrypted binary data will greatly increase the cost of cracking and will be much more secure.

4) reverse control hook:

This. Because it is a duplex connection, the server can push some hook commands, or even direct code, to be executed on the client side. For example, take a screen shot, make a recording, and plant a pony. As long as the user passes the authorization application, the rest is up to you.

The difference between nodejs and websocket

Difference one:

Node is a JavaScript runtime environment based on Chrome V8 engine, a development platform that allows JavaScript to run on the server side.

WebSocket is a protocol for full-duplex communication over a single TCP connection.

Difference two:

Node is used to easily build network applications with fast response and easy to expand.

WebSocket makes it easier to exchange data between the client and the server, allowing the server to actively push data to the client.

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report