eventsource icon indicating copy to clipboard operation
eventsource copied to clipboard

Support for react-native

Open aliassarpro opened this issue 4 years ago • 3 comments

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.

aliassarpro avatar Oct 30 '19 12:10 aliassarpro

Same here

ghasemikasra39 avatar Nov 07 '19 13:11 ghasemikasra39

I don't have much time to maintain this library, and I am completely unfamiliar with react-native.

If somebody submits a pull request with a fix I'll consider handing out commit and release access so they can fix and release this themselves.

aslakhellesoy avatar Nov 07 '19 13:11 aslakhellesoy

Thanks for fast respond aslak. Since I'm working on another project that takes much time from me, I can't help you on that but I hope you leave this issue open to let someone refer & submit a pull request. You can use tags like ( help needed & ... ) too.

aliassarpro avatar Nov 08 '19 05:11 aliassarpro