Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What I'm doing wrong (with react native) #244

Open
lokomass opened this issue Sep 23, 2020 · 3 comments
Open

What I'm doing wrong (with react native) #244

lokomass opened this issue Sep 23, 2020 · 3 comments

Comments

@lokomass
Copy link

lokomass commented Sep 23, 2020

Hi,
I'm using RN with phpsocket.io.
I can connect to socket but any emit works.
What I'm doing wrong ?
Here is my RN code :

<?php require_once __DIR__ . '/man/Autoloader.php'; require_once __DIR__ . '/io/src/autoload.php'; use Workerman\Worker; use PHPSocketIO\SocketIO; $io = new SocketIO(20200); $io -> on('connection', function($socket) { echo "connected\n"; $socket -> on('message', function($data) use($socket) { echo "new message reveived !\n"; $socket -> broadcast -> emit('new message', 'ello'); }); }); Worker::runAll(); ?>

So, on the server side, I always see "connected" in log console.

In React Native, on mount component, I write this :

componentDidMount() { this.socket = io('http://my_dns:20200') this.socket.on("new message", msg => { console.log('new message : '+msg) }) }

If I understand, I'm listening to emit event from server, when user send new message, I write it in console.
And I have a fake button, which send a fake message to server :

this.socket.emit("message", 'hello word')

But the event never received by my server, I never see in console "new message recevied"

I really spend lot of hours to understand what is the mistake.
Can anybody helps me please ?
Thanks a lot

@walkor
Copy link
Owner

walkor commented Sep 24, 2020

It looks like it is phpsocket.io not compatible with your react native socket.io client.
I recommend using nodejs socket.io to replace phpsocket.io.

@binemmanuel
Copy link
Contributor

My application is build in PHP, Flutter and React Native. We are actually moving to React Native from Flutter and we can't get get the React Native app to connect to the Socket Server so we came up with a solution to use SwoolePHP or Node.js socket.io then call our PHP apis from the Node.js Server when required.

@binemmanuel
Copy link
Contributor

binemmanuel commented Mar 10, 2022

Another thing you can do to fix things would be to install an older version of socket.io-client maybe ^1.7.4 because it worked for me.

Go to the package.json file then change the version to ^1.7.4 to look like this;
"socket.io-client": "^1.7.4", then run yarn install or npm update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants