PHP implements sending and receiving messages in online instant messaging system

PHPz
Release: 2023-05-24 12:34:02
Original
1469 people have browsed it

With the changes in people's lifestyles and the continuous development of network technology, instant messaging has become one of the essential communication methods for modern people. Under this trend, various online instant messaging systems have emerged. Since people in different places can exchange messages at the same time, how does the online instant messaging system realize the delivery of messages? This article will introduce how to send and receive messages in an online instant messaging system through the PHP programming language.

  1. The basic principle of implementing an online instant messaging system

The basic principle of an online instant messaging system is to send messages from one client to another. This process contains two main parts:

1.1 Message sending

In the process of sending a message from one client to another client, the following steps need to be completed:

(a) The user enters a message on the client and clicks the send button;

(b) Sends the message to the server through the network;

(c) The server saves the message in the database;

(d) The server sends the message to the target client;

(e) The target client receives the message and processes it.

1.2 Message reception

The client needs to continuously poll the server to check whether there are new messages. When the server has new messages, the client needs to obtain and process the messages in time.

  1. The basic process of using PHP to implement an online instant messaging system

2.1 Create a database

Before implementing an online instant messaging system, you need to create a database for Database where messages are stored. You can use MySQL or other relational databases.

Create the following table in the database:

CREATE TABLE messages (

id int(11) NOT NULL AUTO_INCREMENT,
sender varchar(255) NOT NULL,
receiver varchar(255) NOT NULL,
message text NOT NULL,
created_at datetime NOT NULL,
PRIMARY KEY (id)
Copy after login

);

2.2 Establish server-side code

Server The end code needs to be responsible for receiving the message entered by the user, saving the message to the database, and sending the message to the target client.

First, you need to use the following code to connect to the database:

$conn = mysqli_connect("localhost", "username", "password", "database_name");

Connect Next, you need to write a function to process the message entered by the user:

function save_message($sender, $receiver, $message) {

global $conn;
$t = time();
$created_at = date("Y-m-d H:i:s", $t);
$query = "INSERT INTO messages (sender, receiver, message, created_at) VALUES ('$sender', '$receiver', '$message', '$created_at')";
mysqli_query($conn, $query);
Copy after login

}

This function saves the user name , recipients and message content are saved to the database.

Next, write a function to get and send messages from the database:

function get_messages($receiver) {

global $conn;
$query = "SELECT * FROM messages WHERE receiver='$receiver'";
$result = mysqli_query($conn, $query);
$messages = array();
while ($row = mysqli_fetch_assoc($result)) {
    $messages[] = $row;
}
$json_messages = json_encode($messages);
echo $json_messages;
Copy after login

}

The function starts from Retrieve all messages of the target user from the database and encode them into JSON array format. By calling this function, the server can send the message to the target client.

2.3 Establishing client code

The client code needs to be responsible for sending messages entered by the user to the server and obtaining new messages from the server.

First, you need to use the following code to connect to the server:

var socket = new WebSocket("ws://localhost:8080");

Next, when the user clicks When sending the button, call the following code to send the message to the server:

var sender = "username1";
var receiver = "username2";
var message = "hello, username2";
var data = {"sender": sender, "receiver": receiver, "message": message};
socket.send(JSON.stringify(data));

Finally, you can write functions Poll the server for new messages:

setInterval(function() {

var receiver = "username1";
var data = {"receiver": receiver};
socket.send(JSON.stringify(data));
Copy after login

}, 1000);

This function sends data to the server every second Get new news. This functionality can be more easily achieved using jQuery or other JavaScript libraries.

  1. Summary

This article introduces the method of sending and receiving messages in the online instant messaging system through the PHP programming language. The key to realizing an online instant messaging system is the interaction between server-side and client-side codes, so that messages can be transmitted quickly and efficiently between different clients. In this way, we can implement online instant messaging in web applications to achieve a convenient and fast way of communication.

The above is the detailed content of PHP implements sending and receiving messages in online instant messaging system. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template