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 ways to realize the instant messaging function of the website?

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article introduces the relevant knowledge of "what are the aspects of the implementation of the instant messaging function of the website". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!

Let's first take the chat room as an example, there are many ways to implement web chat room, including the implementation based on ajax technology, the implementation based on Comet (Pushlet) technology, the implementation based on XMPP protocol, and the implementation of XmlSocket and remote shared objects based on flash.

(1) the realization based on ajax technology.

Ajax (Asynchronous JavaScript and XML,Asynchronous javascript and xml), its function is to realize the non-refresh interaction between the page and the server. The basic principle of using ajax to implement a web chat room is to get data from the server through ajax at regular intervals on the page, and then update the page display. This method is simple and clear, but the disadvantage is that the real-time performance is not high.

(2) realization based on Comet technology.

Comet is a new Web application architecture. In applications developed based on this architecture, the server will actively push data to the client program asynchronously without the need for the client to make an explicit request. Comet architecture is very suitable for event-driven Web applications, as well as applications that require high interactivity and real-time performance, such as stock market analysis, chat rooms and Web online games.

Pushlet is a comet implementation (Pushlet is an open source Comet framework): under the Servlet mechanism, data is pushed (push) directly from the server's Java object to the client's page without the help of any Java applet or plug-in. It enables the server side to update the web page of client periodically, which is different from the traditional request/response method.

Pushlet is based on HTTP stream, which is often used in multimedia video and communication applications, such as QuickTime. Contrary to the practice of closing the HTTP connection as soon as the HTTP page is loaded, Pushlet uses HTTP streams to push new data to the client, while the HTTP connection remains open. For more information on how to implement this kind of Keep-alive persistent connection in Java, see "HTTP Persistent Connection" provided by Sun and "HTTP1.1 Specification" of W3C.

(3) implementation based on XMPP protocol

XMPP (Extensible message processing Field Protocol) is a protocol based on XML. It is a communication protocol designed for timely communication systems. It is used for instant messaging and online field detection. It facilitates quasi-immediate operation between servers. This protocol may eventually allow Internet users to send instant messages to anyone else on the Internet, even if their operating systems and browsers are different. The predecessor of XMPP is Jabber, a network instant messaging protocol generated by open source grouping. The famous open source chat system server Openfire is a Jabber server based on XMPP protocol.

You can interact with Jabber server through Flash or ajax to realize the function of webIM

(4) implementation of XmlSocket based on flash

Flash Media Server is a very powerful streaming media server, which is based on rtmp protocol and provides strong streaming media interaction. In FMS, a mechanism for remote shared objects (SharedObject) is provided, and clients can create and connect to remote shared objects on the server side. There can be many clients connecting to the same remote shared object. If any client modifies the shared object, the server will send the modification information of the shared object to all other clients connected to the shared object. This remote object sharing mechanism can achieve the following functions: remote control slide show text chat network war remote selection and playing songs live auction customer service application.

Remote shared objects are suitable for implementing group chat functions in web chat rooms. Create a remote shared object for each group, so that any user who sends a message on the group can automatically send it to all group members through the server.

It is impractical to use remote shared objects to chat alone. For the implementation of single chat, we need to use socket. The client chats privately with other clients through the socket server. Chat messages are forwarded through the socket server.

This is the most efficient way to implement web chat rooms.

Instant messaging system architecture

This paper briefly introduces the architecture of IM system for large-scale commercial applications. An important point in designing this architecture is low coupling, which designs the whole system into multiple separate subsystems. I divided the whole system into the following parts: (1) status messaging system (2) friend system (3) P2P system (4) other extended business systems

Let's take a look at the status message system.

(1) connd

Client access server, which can support UDP or TCP. Generally speaking, it is recommended to choose TCP first. Multiple connd can be deployed. When client is connected, a simple DNS polling method can be used to achieve load balancing. The connd function is to maintain connections and forward message packets.

(2) pconnd

Proxy connd, proxy access server, is an extension of connd, in addition to the function of connd, support server access, such as web server.

(3) msgd

Message processing server, the main functions are user state management, message forwarding (including rationality verification) and offline message preservation.

It is said that the process of notifying the status of all friends after a user has successfully logged in. In the system I designed, the user status is simply regarded as similar to a text chat message. The following user U's online process, he has friends F1, F2.

(1) connd receives the U online message and sends the message to the msgd where U is located.

(2) msgd gets friends of U, F1, F2; if F1, F2 and U are not on the same msgd, msgd forwards the message to the msgd of F1 and F2 through connd.

(3) the final msgd sends the launch notification to F1 and F2 through connd.

How does msgd U get the latest friends? I would like to describe this problem in detail.

The user's friend data is in another subsystem: the friend subsystem. Msgd through TCP (why use TCP? Take the initiative to get it from the friend system. At the same time, msgd also caches a copy of friend data. When msgd gets a user's friend, if the cache is up-to-date, take it directly from cache, otherwise it will be taken from the friend subsystem. Now the key question comes out, how to make sure that the user's friends are up to date? We should flexibly adopt different methods according to the different characteristics of different business. Look at an efficient way to deal with it:

(1) the friend subsystem calculates a hash value for each user's friend (you can use MD5).

(2) when client gets friends, it also gets this hash value; when sending messages related to friends, it brings the hash value to msgd.

(3) msgd also gets this hash value when it first acquires a user's friend from the friend subsystem. For example, when you want to forward a status message and get a friend, compare the hash2 brought by client with its own hash3.

Business like IM is characterized by very little writing of friend data and a lot of reading, which is basically negligible relative to the consumption of reading. Using the above method, the hash values of the two are basically the same each time, and the friend data is taken directly from cache. This method can also be introduced into other application businesses. It is recommended not to rudely get similar friend data across processes every time.

This is the end of the content of "what are the aspects of the implementation of the instant messaging function of the website"? thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Servers

Wechat

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

12
Report