Faye, czyli łatwe pubsub z websockets
fayeczyli łatwe pub/sub z websockets
View Slide
fayewiadomości pub/subserwer: node.js / ruby (rack)backend: ram / redisklient: ruby / jsfallback do long-polling
faye:architektura
faye + redis
private_pubgem dla Railsułatwia używanie Fayeprywatne kanałydwa tryby publikowania: json, eval
liveblogstworzenie liveblogawysyłanie wiadomościwyświetlanie livebloga
1 # Run with: rackup faye.ru -s thin -E production2 require "yaml"3 require "faye"4 begin5 require "private_pub"6 rescue LoadError7 require "bundler/setup"8 require "private_pub"9 end1011 PrivatePub.load_config("config/private_pub.yml",ENV["RAILS_ENV"] || "development")12 run Faye::RackAdapter.new(:mount => "/faye", :timeout =>45, :extensions => [PrivatePub.faye_extension])faye.ru
1 development:2 server: "http://localhost:9292/faye"3 secret_token: "secret"4 test:5 server: "http://localhost:9292/faye"6 secret_token: "secret"7 production:8 server: "http://liveblogs.code-fu.pl:9292/faye"9 secret_token: "sekret ;)"config/private_pub.yml
1 class Stream2 include Mongoid::Document3 include PrivatePub::ViewHelpers45 belongs_to :user6 embeds_many :messages7...2425 def create_message_and_publish(author, message)26 if self.active27 m = self.messages.create(a: author, m: message)28 publish_to "/streams/#{self.id}", m.to_json29 end30 end31 endapp/models/stream.rb
1 = subscribe_to "/streams/#{@stream.id}"23 %ul#stream4 .new_messages5 .previous_messages6 - @messages.each do |message|7 = render 'liveblogs/message', :message => message89 :javascript46 PrivatePub.subscribe("/streams/#{@stream.id}", function(data, channel) {47 data = jQuery.parseJSON(data);48 $(".new_messages").prepend("" +49 data.t.slice(11,16) + "\n" +50 "" + data.a + "\n" +51 data.m + "").children(":first").hide().fadeIn('slow');52 });app/views/liveblogs/show.html.haml
demo
publish_to + render<% publish_to "/messages/new" do %>$("#chat").append("<%= escape_javascript render(@messages) %>");<% end %>
pytania?http://faye.jcoglan.com/https://github.com/ryanb/private_pubhttp://liveblogs.code-fu.pl/http://speakerdeck.com/u/grk/p/faye