Skip to content

Instantly share code, notes, and snippets.

@jsdf
Created March 31, 2015 10:06
Show Gist options
  • Save jsdf/7b71a590caaa6dccbd31 to your computer and use it in GitHub Desktop.
Save jsdf/7b71a590caaa6dccbd31 to your computer and use it in GitHub Desktop.
Use Faye pubsub with React Native
// make sure you npm install 'url' (the browserify port of the node api url module)
var url = require('url')
var Faye = require('faye/browser/faye-browser')
var API_URL = 'http://localhost:8000/faye'
// initialise fake window.location - same origin as api host
// required to use some faye transports which expect to deal with same-origin policy.
// url.parse return value looks kind of like window.location, if you squint just right
window.location = url.parse(API_URL)
var client = new Faye.Client(API_URL)
client.publish('/blah', {}) // works
@terrysahaidak
Copy link

Hey. Does it really work? Because I spend few hours to get it work.. And it still not working.

@gautamg795
Copy link

@terrysahaidak: Try also setting window.location.port="". That fixed it for me.
The problem is that Faye tries to check if the server is same-origin; if not, it tried to use JSON-P for the handshake (which won't work on React Native). The same-origin check compares hostname, port, and scheme—url.parse leaves port as null when not provided, whereas the empty string is expected.

@brianinator
Copy link

Almost a year old, but what's the benefit of doing it like this? and what about using const Faye = require('faye') and not the browser?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment