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 realize the Pub/Sub of Redis with WebSockets as the front-end EventMachine-like implementation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces Redis's Pub/Sub to WebSockets as the front end of the EventMachine-like implementation, the content is very detailed, interested friends can refer to, hope to be helpful to you.

EventMachine is an event-driven network library of Ruby, an EventMachine-like implementation with Redis's Pub/Sub mechanism as the back end and WebSockets as the front end. The following editor will explain how the Pub/Sub of Redis uses WebSockets as the front-end EventMachine-like implementation?

How to realize the Pub/Sub of Redis with WebSockets as the front-end EventMachine-like implementation

Front-end code, create Socket to connect to local port 8081, and print the message to the specified div when a message push is coming:

How to realize the Pub/Sub of Redis with WebSockets as the front-end EventMachine-like implementation

Backend code:

Require'redis'

Require'em-websocket'

SOCKETS= []

@ redis=Redis.new (: host= > '127.0.0.1) > 6379)

# CreatingathreadfortheEMeventloop

Thread.newdo

EventMachine.rundo

# Createsawebsocketlistener

EventMachine::WebSocket.start (: host= > '0.0.0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 8) do = > 8081) do | ws |

Ws.onopendo

# WhensomeoneconnectsIwanttoaddthatsockettotheSOCKETSarraythat

# Iinstantiatedabove

Puts'creatingsocket'

SOCKETS

<   end   ws.onclosedo   #UponthecloseoftheconnectionIremoveitfrommylistofrunningsockets   puts'closingsocket'   SOCKETS.deletews   end   end   end   end   #Creatingathreadfortheredissubscribeblock   Thread.newdo   @redis.subscribe('ws')do|on|   #Whenamessageispublishedto'ws'   on.messagedo|chan,msg|   puts"sendingmessage:#{msg}"   #Sendoutthemessageoneachopensocket   SOCKETS.each{|s|s.sendmsg}   end   end   end   sleep   开启8081端口接受连接,同时连到Redis上订阅ws这个key的消息   当前后端都启动并连接上后,你就可以用如下代码往Redis的ws这个key上写消息,页面上就能看到push过来的消息了:   require'redis'   @redis=Redis.new(:host=>

'127.0.0.1) > 6379)

@ redis.publish'ws','Somethingwitty'

On Redis's Pub/Sub how to WebSockets as the front end of the EventMachine-like implementation is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Database

Wechat

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

12
Report