Skip to content

Support for react-native #132

Open
Open

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.

Activity

  1. ghasemikasra39 commented on Nov 7, 2019

    @ghasemikasra39

    Same here

  2. aslakhellesoy commented on Nov 7, 2019

    @aslakhellesoy
    Contributor

    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.

  3. aliassarpro commented on Nov 8, 2019

    @aliassarpro
    Author

    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.

  4. rexxars commented on Dec 7, 2024

    @rexxars
    Member

    I'm curious if the new version (eventsource@^3) works in React Native. It does depend on quite a few "modern" APIs that may or may not exist there. Would love to hear back from someone who can give it a try!

  5. rexxars commented on Mar 11, 2025

    @rexxars
    Member

    I gave React Native a try - unfortunately there are quite a few blockers:

    • fetch does not return a streamable body (response.body is undefined)
    • Missing Event, MessageEvent and EventTarget APIs
    • Missing TextDecoderStream API

    I'd prefer to not have a huge amount of legacy code for a single environment in this module.

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions