Search code examples
rubychatpublish-subscribefayebayeux

Adding custom fields to a subscription response using the faye ruby server


I'm trying to write a proof-of-concept chat application using faye (the chat example which is included in the distribution).

In my proof-of-concept I want to send a full history of a chat channel when a client subscribes to a channel. My current idea was to implement this using a custom field in the subscription response message.

After checking the bayeux protocol definition it seems, that a 'ext' field is allowed in a subscription response message.

But I was unable to add anything to this ext field using a server extension.

class ServerLog
  def incoming(message, callback)
    puts " msg: #{message}"
    unless message['channel'] == '/meta/subscribe'
      return callback.call(message)
    end

    # the following line changes absolutely nothing
    message['ext'] = 'foo'

    callback.call(message)
  end
end

App.add_extension(ServerLog.new)

Although the setting of the ext field doesn't crash the server, it has absolutely no effect on the subscription response message. I've even checked using Wireshark (just to be sure the js client doesn't ignore some fields).


Solution

  • My mistake was using the 'incoming' method, and not 'outgoing'.

    class ServerLog
      def outgoing(message, callback)
        puts " out: #{message}#"
        unless message['channel'] == '/meta/subscribe'
          return callback.call(message)
        end
    
        if message['subscription'] == '/chat/specialchannel'
          message['ext'] ||= {}
          message['ext']['specialattribute'] = 'special value'
        end
    
        callback.call(message)
    
      end
    end
    
    App.add_extension(ServerLog.new)
    

    This example will add the specialattribute to the ext field of the subscription response message (if the channel is /chat/specialchannel).