--- title: 12th 鐵人賽 Day_25 action cable? (2) Web Socket ? tags: 12th 鐵人賽 --- # 12th 鐵人賽 Day_25 action cable? (2) Web Socket ? 嗨!各位朋友大家好,打給後,歹嘎吼,胎尬喉,我是阿圓,一樣有請今天的one piece:   (海軍將懸賞單廣播出去!就像 Pub/Sub 模式!) 昨天講到了早期的推播技術,而隨著這樣的需求越來越大,不同於 HTTP 協定的 Web Socket 便出現了! # Websocket ? 先來了解一下甚麼是Websocket ,我們請wiki大大出來說幾句話: >Websocket是一種與HTTP不同的協定。兩者都位於OSI模型的應用層,並且都依賴於傳輸層的TCP協定,雖然它們不同,但Websocket通過HTTP埠80和443進行工作,並支援HTTP代理和中介 (有點像是升級版的HTTP,因為他們還是用同一個 port 工作。) 一言以蔽之,Websocket 是一種讓瀏覽器和伺服器,進行持續性的雙向連線溝通,允許 server 隨時的、主動的向 client 端發送 response 的一種通訊協定  (Websocket 是通過 HTTP/1.1 協定的101狀態碼進行交握(Handshaking),上圖其實就是告訴 server ,我們要走 Websocket 協定,有關更多 Websocket 的詳細介紹,請參考wiki) 利用 Websocket 來通訊的方式有很多種,而 action cable 是採用Pub/Sub的方式。 # [Pub/Sub ?](https://en.wikipedia.org/wiki/Publish%E2%80%93subscribe_pattern) 一般而言,發送訊息的方式以兩種,而Pub/Sub模式,並不是單純的直接將 publisher 發送出去的訊息丟給 subscriber ,而是透過代理層(message broker)的方式將訊息廣播給subscriber。 換句話來說, publisher 跟 subscriber 之間根本不需要知道彼此是誰, 一切都交由中間層的 message broker 來處理。  (相較於 Pub/Sub 另一種模式稱為 [observer pattern](https://en.wikipedia.org/wiki/Observer_pattern#:~:text=The%20observer%20pattern%20is%20a,calling%20one%20of%20their%20methods.),有興趣請參考 wiki) 感謝各位看到這邊,明天我們就來實際的看看 Rails 的 action cable 是如何運作的吧!若有任何建議,請各位不吝指教!我們明天見!  --- #### 參考文章 [官方文件](https://guides.rubyonrails.org/action_cable_overview.html) [Difference Between Pub-Sub Pattern and Observable Pattern](https://medium.com/easyread/difference-between-pub-sub-pattern-and-observable-pattern-d5ae3d81e6ce) [Ruby on Rails 實戰聖經](https://ihower.tw/rails/actioncable.html)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up