Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React native and socket.io node not working

Below is my react native component and node server.js code that I am trying to connect to my node websocket backend.

My react code is running on the same computer as the server. I have found so many varying answers on here, and github, none of which I can get working.

I also found this question which was never answered, and this question has an answer, which I cannot get working (was asked over a year ago)

I have found this article and tried to amend my code based on these guidelines but this did not work.

react code

import React from 'react';
import { StyleSheet, Text, View, Image } from 'react-native';

const io = require('socket.io-client/socket.io');
let socket = io('http://localhost:3000');

export default class App extends React.Component {

    constructor(props) {
        super(props);
        console.log(socket);
    }

    render() {
        return (
            <View>
                <Text>Websocket</Text>
            </View>
        );
    }
}

server.js

const express = require('express');
const http = require('http')
const socketio = require('socket.io');

const app = express();
const server = http.Server(app);
const websocket = socketio(server);
server.listen(3000, () => console.log('listening on *:3000'));

console.log(websocket)

// The event will be called when a client is connected.
websocket.on('connection', (socket) => {
  console.log('A client just joined on', socket.id);
});

I am using the following versions of packages

"expo": "^16.0.0",
"react": "16.0.0-alpha.6",
"react-native": "^0.43.4",
"socket.io-client": "^1.7.3"
like image 662
peter flanagan Avatar asked May 01 '17 18:05

peter flanagan


People also ask

Can you use node with react native?

Installing dependencies. You will need Node, the React Native command line interface, a JDK, and Android Studio. While you can use any editor of your choice to develop your app, you will need to install Android Studio in order to set up the necessary tooling to build your React Native app for Android.

Does socket.io require node JS?

In this guide we'll create a basic chat application. It requires almost no basic prior knowledge of Node. JS or Socket.IO, so it's ideal for users of all knowledge levels.

Can you use React and node together?

You can definitely use Node JS with React framework. In fact, Node JS is known to be the most suitable platform for hosting and running web servers for applications built on React. The two primary reasons for this are; Node uses a node package manager or an NPM to install all new updates and packages.


1 Answers

I believe it should be

const io = require('socket.io-client');

Which does work for me.

I remember running into these sorts of issues when using react native. A lot of socket.io tutorials (including the one on their page) assume you're using an older style of importing JS via script tags in an HTML doc. It looks like socket.io changed the namespace as I do remember it being socket.io-client/socket.io some time ago if memory serves...

like image 95
klvs Avatar answered Sep 19 '22 15:09

klvs