In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Today's small series will bring you an article introducing Redis to achieve chat room functions. Xiao Bian thinks it is quite practical, so share it for everyone to make a reference. Let's follow the editor and have a look.
First of all, let's introduce a few commands about pub/sub.
released a message
publish channel message Publish a message to a specified channel
Suppose there is a group of riders, and a user posts a message.
127.0.0.1:6379> publish car:fans hello,everyone(integer) 0
The return value of this command is the number of subscribers subscribed to this channel. As you can see, no one has subscribed to this channel yet.
subscribe message
subscribe channel [channel] Subscribers can subscribe to one or more users
127.0.0.1:6379> subscribe car:fansReading messages... (press Ctrl-C to quit)1) "subscribe"2) "car:fans"3) (integer) 1
At this point, a user has joined the group of riders. Gradually, more and more people joined the group.
When a user posts a message, the rest of the group can see it.
127.0.0.1:6379> publish car:fans 'How are you'(integer) 2127.0.0.1:6379> subscribe car:fansReading messages... (press Ctrl-C to quit)1) "subscribe"2) "car:fans"3) (integer) 11) "message"2) "car:fans"3) "How are you"
Note: When the client executes the subscription command, it enters the subscription state and can only execute pub/sub related commands. In addition, users who join a new group cannot see previous messages because this mechanism does not persist messages. Although the function is limited, the victory is simple enough.
More to say, the function is not the more the better, mainly suitable for the scene. If you can do it, the simpler the better.
View Subscriptions
pubsub numsub [channel ...]
Want to see how many users are in the group
127.0.0.1:6379> pubsub numsub car:fans1) "car:fans"2) (integer) 2
The group currently has 2 users.
unsubscribe
unsubscribe [channel [channel ...]]
redis-cli UNSUBSCRIBE1) "unsubscribe"2) (nil)3) (integer) 0
actual combat
After introducing the above knowledge, we can complete a chat room function.
The pseudo-code for publishing messages is quite simple. The pseudo-code for publishing messages is as follows:
function publist ($chanel, $message){ $redis->publist($channel, $message);}
The pseudocode to get the message is as follows:
//Set php script execution time set_time_limit(0);//Set socket connection timeout ini_set ('default_socket_timeout', -1); //Declare channel name $channelName = "testpubsub"; try { $redis = new \Redis(); $redis->pconnect('localhost', 6379); $redis->subscribe([$channelName], function ($redis, $channel, $msg) { echo 'channel:' . $channel . ',message:' . $msg . PHP_EOL; });} catch (\Exception $e) { echo $e->getMessage();}
Here are a few caveats:
Consumers need to create redis long connections,
Set set_time_limit and default_socket_timeout to ensure that php does not timeout when blocking get messages and socket connections do not timeout
About Redis to achieve chat room function to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you like this post, share it with more people.
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.