Created
March 31, 2015 10:06
-
-
Save jsdf/7b71a590caaa6dccbd31 to your computer and use it in GitHub Desktop.
Use Faye pubsub with React Native
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
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
@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
leavesport
asnull
when not provided, whereas the empty string is expected.