Upgrade to Pro — share decks privately, control downloads, hide ads and more …

realtime_rails

mgi166
September 25, 2015

 realtime_rails

mgi166

September 25, 2015
Tweet

More Decks by mgi166

Other Decks in Programming

Transcript

  1. Realtime Rails • WebSocket ͷ֓ཁ • ͲΜͳࣄ͕Ͱ͖Δͷ͔ • WebSocket +

    Rails ͷαϯϓϧίʔυ • actioncable (websocket-rails ΋গ͠) • ՝୊ʹͳΓͦ͏ͳ఺
  2. WebSocket ͱ͸ • αʔόʔͱϒϥ΢βؒͰϦΞϧλΠϜͰ৘ใ Λ΍ΓͱΓ͢Δن֨ͷ͜ͱ • chat, online game, push

    ௨஌౳ • ࠷ۙͷϒϥ΢β͸طʹ࣮૷ࡁΈ • αʔόʔଆͷ༻ҙΛ͢Ε͹ɺ࣮ݱՄೳ
  3. WebSocket ͱ͸ • server ʹ request ͯ͠ response Λฦ͢ɺͱ͍͏ http

    ͷن֨Ͱ͸࣮ݱͰ͖ͳ͍ • αʔόʔͱϒϥ΢βؒͰৗʹ৘ใΛ΍ΓͱΓ͠ଓ ͚Δ • ϒϥ΢βͰೖྗͨ͠࠲ඪΛαʔόʔଆʹ • αʔόʔଆʹ౉ͬͨ࠲ඪΛϒϥ΢βʹ
  4. WebSocket ք۾ͷྺ࢙ • node.js ͕͍͢͝Β͍͠ (2013 ೥ࠒ) • node.js ʹΑΔ

    c10k ໰୊ͷղফ • socket.io • http://socket.io/demos/weplay/ • ϒϥ΢β͕ websocket ରԠ࢝͠ΊΔɻnginx ΋ • ←ࠓίίʂ
  5. ͿͬͪΌ͚ rails ΋ରԠ͢Δ • rails5 ͷ ActionCable • rails +

    WebSocket Λ΍ͬͯ͘ΕΔ • https://github.com/rails/actioncable • websocket-rails ͱͷҧ͍͸αʔόʔ • websocket-rails: thin, actioncable: ͳΜͰ΋
  6. XFCTPDLFU TFSWFS 4. Push change to users 1. Create new

    message 2. Publish changes to Redis 3. Publish change to subscribers
  7. var dispatcher = new WebSocketRails('localhost:3001/websocket'); var CommentForm = React.createClass({ onSubmit:

    function(e) { e.preventDefault(); dispatcher.trigger('new_message', { text: text }); this.props.createMessage({ text: text }); }, render: function() { return ( <form className="comment-form" onSubmit={this.onSubmit}> <textarea placeholder="write something..." ref="text"/> <input type="submit" value="submit" /> </form> ); } }); WebSocket ʹΠϕϯτΛ௨஌ WebSocket Ͱαʔόʔଆ ʹ௨஌͢Δ Rails ͷ api Λ ajax Ͱୟ͘
  8. # config/events.rb WebsocketRails::EventMap.describe do subscribe :new_message, to: BroadcastController, with_method: :new_message

    end # app/controllers/broadcast_controller.rb class BroadcastController < WebsocketRails::BaseController def new_message WebsocketRails[:chat_channel].trigger(:new_message, message) end end WebSocket ͷ controller Event ໊ͱ controller Λهड़ (routing) :chat_channel ͱ͍͏νϟωϧʹରͯ͠ broadcast ͢Δ
  9. var dispatcher = new WebSocketRails('localhost:3001/websocket'); var channel = dispatcher.subscribe(‘chat_channel’); var

    Contents = React.createClass({ componentDidMount: function() { channel.bind('new_message', function(message){ var messages = this.state.messages; messages.unshift(message); this.setState({ messages: messages }) }.bind(this)); }, render: function() { return ( <div class=“content"> … </div> ); } ௨஌͕͖ͨΒɺDOM ʹ൓ө :chat_channel ͱ͍͏ νϟωϧΛ js ଆͰߪಡ νϟωϧʹ௨஌͕དྷͨΒ callback ͕ݺ͹Εͯ state Λߋ৽
  10. rails (websocket-rails actioncable) ͷϙΠϯτ • EventMachine Λ৴༻͢Δ͔Ͳ͏͔ • Thin ͸

    EventMachine Ͱ࣮૷͞ΕͯΔ • puma ΋ EventMachine ʹͪΐͬͱґଘͯ͠Δ • ࠇ͍ᷚ͹͔Γࣖʹͯ͠طʹک͑ͯΔ • actioncable ͳΒαʔόʔͷىಈΛࣗ༝ʹఆٛͰ͖Δ෼ɺ ॊೈੑ͕ߴ͍ҹ৅Λड͚ͨ
  11. εέʔϥϏϦςΟ • EventMachine ͰͲ͜·Ͱεέʔϧ͢Δ͔ • 1000 ίωΫγϣϯ͔Βਏ͘ͳΔͱ͍͏ᷚ • puma ͸Θ͔ΒΜ

    • rails ͔Β੾Γସ͑Δͱεέʔϧ͠·ͨ͠ɺͱ͍͏࿩͸Α͘ฉ͘ • http://www.infoq.com/jp/news/2012/10/Ruby-on-Rails-Node-js-LinkedIn • http://www.infoq.com/jp/news/2014/01/paypal-java-javascript • http://www.infoq.com/jp/news/2015/07/parse-moved-ruby-go
  12. ࢑ఆ݁࿦ • ͋Δఔ౓ͷ઀ଓ਺Λࡹ͘ϦΞϧλΠϜ web ͕ඞཁͳ Βɺrails Ҏ֎Λݕ౼ͨ͠΄͏͕͍͍͔΋ • WebSocket server

    ͸૿͖͍͑ͯͯΔͷͰɺબ୒ࢶ͸๛ ෋ɻͲ͏΍Δ͔͸ॊೈʹબ୒͢Δ • rails Ҏ֎ͩͱ phoenix ͸஫໨ג • actioncable ͳΒ͖ͬͱεέʔϧͯ͘͠ΕΔ…!