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

How to receive data in Real time by php

2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how to receive data in real time by php". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to receive data in real time by php"!

1. Publish subscribed Pub/Sub

Publish subscription (Pub/Sub): a widely used communication model that uses events as the basic communication mechanism and provides a loosely coupled interaction pattern required by large-scale systems: a subscriber (such as a client) expresses an event or a class of events that it is interested in receiving in the form of an event subscription. Publishers (such as servers) can notify relevant subscribers of events of interest to subscribers at any time-is it born to the same mother as the observer pattern in the design pattern?

Redis's Pub/Sub is very simple, stable and fast. Support pattern matching, can subscribe and cancel channels in real time. For example, there are many examples of real-time chat systems and mass chat messages built with Pub/Sub.

Redis clients can subscribe to any number of channels.

Redis provides publish and subscribe function, which can be used for message transmission. The publish and subscribe mechanism of Redis consists of three parts, publisher, subscriber and Channel.

Both publishers and subscribers are Redis clients, while Channel is the Redis server. The publisher sends a message to a channel, and subscribers to that channel can receive the message. This publishing and subscribing mechanism of Redis is similar to topic-based publishing and subscribing, and Channel is equivalent to topics.

two。 Usage scenarios for publish subscriptions

The most classic application scenarios are Weibo and official account. As long as fans follow (subscribe) someone's Weibo or official account, and the Weibo or official account has only status updates, the message will be pushed (posted) to fans.

The process of publishing and subscribing is for our publisher to establish a relationship with our channel and then send this message over a long connection. At the same time, we also have a subscriber, we also need to subscribe to our content, if we subscribe, then when our channel receives a message, we will push the message to the subscriber in time.

So the connection here will involve our long connection and short connection. What's the difference between them?

Real-time publish and subscribe based on 3.PHP

Subscribe (message subscriber sub.php): if you execute the sub.php command on the terminal, you will be in a listening state, waiting for the publisher to publish the message for processing.

$redis=new Redis ()

$res=$redis- > pconnect ('127.0.0.1 records, 6379jin0)

$redis- > subscribe (array ('luke'),' callback')

/ / callback function, where processing logic is written

Function callback ($instance, $channelName, $message) {

Echo $channelName, "= = >", $message,PHP_EOL

}

Publish (message publisher pub.php):

$redis=new Redis ()

/ / the first parameter is the ip of the redis server, and the second is the port

$res=$redis- > connect ('127.0.0.1 dollars, 6379)

/ / luke is the name of the published channel and hello,world is the published message

$res=$redis- > publish ('luke','hello,world')

4.redis can implement the functions of publish / subscribe and message queuing. The difference between the two is:

1) redis subscribers can have multiple subscribers and can process published messages at the same time. If the subscribers are not online (the service is not started), the message will be lost and the message will not be persisted. Publish and subscribe are executed together, missing a directly ignore this message

2) message queuing can only be processed by one client, and after processing, the message is marked or deleted, even if the server does not start the message, it will not be lost.

3) the usage scenarios of the two should be determined according to the accuracy and sensitivity of the business data. For example, logs can be implemented by redis publish and subscription, and it has nothing to do with loss at all.

At this point, I believe you have a deeper understanding of "how to receive data in real time by php". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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