Skip to content

Support for react-native #132

@aliassarpro

Description

@aliassarpro

Hi,
I think it is so painful that such a useful module can't be implemented in react-native.

What I did:
I made a blank project with react native and used the module just like every other api:

import {
	View,
} from 'react-native';
var EventSource = require('eventsource')
export default class App extends Component {
	componentDidMount() {
		var es = new EventSource('http://localhost:8080/sse')
		es.addEventListener('server-time', function (e) {
			console.log(e.data)
		})
	}
	render() {
		return (
			<View/>
		);
	}
}

then there was below error.

error: bundling failed: Error: Unable to resolve module urlfromnode_modules\eventsource\lib\eventsource.js: url could not be found within the project.

I also tried using compiled polyfill or compiling with browserify but another error appeared:

1

Possible Solution
the error when I use your module (not the polyfill) & errors after that was solved after I installed 'url' & other modules like 'http' & 'https' .

then in the bundler I saw below error just as I expected since react-native don't support node core modules due to difference between binary files.

error: bundling failed: Error: While trying to resolve module httpsfrom file\node_modules\eventsource\lib\eventsource.js, the package \node_modules\https\package.jsonwas successfully found. However, this package itself specifies amain module field that could not be resolved (\node_modules\https\index.js)

In the link below I found a way to compile required http, https, base64-js, buffer, ieee754 modules that are available in the node core modules.

After that the http module compiled file with browserify required a file named './decode' and some other files that I couldn't found.

To Reproduce

Just build a react-native project & copy the code into it.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions