How to connect socket.io with a Heroku-deployed React Native app?

风格不统一 提交于 2020-01-16 08:45:15

问题


I have almost googled my fingers off trying to figure this out. It seems a lot of the existing info on connecting socket.io with React Native is outdated, or maybe I'm just interpreting things wrong?

I've managed to get the client-side connected (I'm getting the client console logs when I connect to my app). It seems to be the server-side that's giving me issues. Why is the data being emitted from the client not showing up as a log in my terminal? None of the related console.logs in my server.js are logging but the App.js console.logs are registering.

Edit: Here is my full App.js file:

import Expo from 'expo';
import React from 'react';
import { Dimensions, StyleSheet, Text, View } from 'react-native';

import store from './src/store';
import { Provider } from 'react-redux';


// window.navigator.useragent = 'react-native'; -> not necessary anymore?



const ROOT_URL = 'https://myherokudomain.herokuapp.com';

const io = require('socket.io-client/dist/socket.io');
const socket = io.connect(ROOT_URL);


socket.on('connect', () => {
     console.log('Connected to server');
});


socket.on('example', (data) => {
    console.log(data);

    socket.emit('my other event', { my: 'data' });
});


socket.on('disconnect', () => {
    console.log('Disconnected from server');
});

export default class App extends React.Component {


render() {

// const MainNavigator = my react-navigation system

return (
  <Provider store={store}>
    <View style={styles.container}>
      <MainNavigator />
    </View>
  </Provider>
  );
 }
}

Edit: Here is my full server.js file:

const config = require('./config/config');
const { mongoose } = require('./db/mongoose');
const express = require('express');

const cors = require('cors');


const app = express();
const port = process.env.PORT;

// ************ Include and use separate routes file
app.use(require('./routes/routes'));
// ************


//Cross-Origin resource sharing. cors library solves CORS problems.
app.use(cors());


//***********
/* Chat server code*/


// enabled heroku session affinity: 
// see https://devcenter.heroku.com/articles/session-affinity
// to enable:   heroku features:enable http-session-affinity
// to diable:   heroku features:disable http-session-affinity

const socketIO = require('socket.io');
const http = require('http');


const server = http.createServer(app);

const io = socketIO(server, { origin: "*:*" });
//********** */


io.on('connection', (socket) => {
    console.log('A client just joined', socket.id);

    socket.emit('example', { hello: 'world' });

    socket.on('my other event', (data) => {
        console.log(data);
    });


    socket.on('disconnect', () => {
        console.log('User was disconnected');
    });
});


server.listen(port, (err) => {
    console.log(`started on port ${port}`);
});



module.exports = { app };

I am getting the console logs on the client side just fine (for instance, the "connected to server" and "hello: world" stuff is showing up when I open my app on expo. But I am not getting the server-side console logs.

What am I doing wrong - how do I get socket.io fully working with a deployed React-Native app?

I would really appreciate any help at all! I've been stuck on this forever.


回答1:


I'm assuming all the code works, just not the logging since that's all you're asking about. The problem is Node doesn't output to your browser's console.

If it's deployed on heroku then you should see everything being logged there, otherwise you can use libraries like https://github.com/node-inspector/node-inspector to output to your browser.




回答2:


You're not getting the server-side console logs because 1.) They're only logging on the server, and 2.) You're not emitting them, if you do actually want to send the data back.



来源:https://stackoverflow.com/questions/50010719/how-to-connect-socket-io-with-a-heroku-deployed-react-native-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!