Skip to content
Eric Draut edited this page Dec 26, 2015 · 20 revisions

Foreign Office is a client push library for Rails. It allows you to have side effects on your client while keeping all business logic on your servers.

Broadcasters and Listeners Foreign Office consists of broadcasters, usually service models that you create to broadcast state from models on the server, and listeners, which allow you to receive content or execute common behaviors on the client based on the state that is broadcast by the server.

Use It

class User < ActiveRecord::Base
  attr_accessor :load_friend
  has_many :friendships

  def serialize
    self.attributes.merge friend_count: self.friendships.count, load_friend: self.load_friend
  end
end

class AddFriend
  def init(initiator,new_friend)
    @initiator, @new_friend = initiator, new_friend
  end

  def call
    @friendship = Friendship.create(initiator: @initiator, friend: @new_friend)
    @recipient.load_friendship @friendship
    @recipient.broadcast_change
    @new_friend.load_friendship @friendship
    @new_friend.broadcast_change
    @friendship
  end
end
Clone this wiki locally