Kurt Russell
Kurt Russell

Reputation: 235

Faye Server Error - undefined method - incoming

In faye.ru I have this code with faye_token:

require 'faye'
require File.expand_path('../config/initializers/faye_token.rb', __FILE__)

Faye::WebSocket.load_adapter('thin')

class ServerAuth
  def incoming(message, callback)
    if message['channel'] !~ %r{^/meta/}
      if message['ext']['auth_token'] != FAYE_TOKEN
        message['error'] = 'Invalid authentication token.'
      end
    end
    callback.call(message)
  end
end

faye_server = Faye::RackAdapter.new(:mount => '/faye', :timeout => 45)
faye_server.add_extension(ServerAuth.new)
run faye_server

If I run this code in terminal:

rackup faye_alt.ru -E production -s thin

And if I refresh a localhost web-seite - I become this error:

[ERROR] [Faye::RackAdapter] undefined method `[]' for nil:NilClass

Any ideas? Please, help!

Upvotes: 2

Views: 685

Answers (2)

Archie Reyes
Archie Reyes

Reputation: 527

The message['ext'] is nil. You can check if your message['ext'] is nil by

def incoming(message, callback)
  if message['channel'] !~ %r{^/meta/}
    if message['ext'].nil? || message['ext']['auth_token'] != FAYE_TOKEN
      message['error'] = 'Invalid authentication token'
    end
  end
callback.call(message)
end

above answer is wrong you need to clear out the message['ext']['auth_token'] is to clear out the auth token so it is not leaked to the client.

def outgoing(message, callback)
  if message['ext'] && message['ext']['auth_token']
    message['ext'] = {} 
  end
callback.call(message)
end

Upvotes: 2

Keviv Vivek
Keviv Vivek

Reputation: 222

Here message['ext'] is nil, probably because you are not passing ext auth token from your browser end when you make subscription or ping to faye server.

You might want to look into

http://faye.jcoglan.com/browser/extensions.html

You have to make something similar to this..

outgoing: function(message, callback) {
  message.ext = message.ext || {};
  message.ext.token = your_token;
  callback(message);
}

Upvotes: 0

Related Questions