React Native

React Native and Socket.io

React Native finally supports Websockets. Which is awesome, however there is one gotcha with socket.io.

1) npm install socket.io-client. This will just pull down the socket.io javascript client

2) Everything works great but there is one issue that window.navigator.userAgent doesn't exist. Socket.IO checks to deal with some browser incosistencies but we don't care. So all we have to do is create it. However make sure you require react-native first!

Just like so

window.navigator.userAgent = "react-native";

var io = require('socket.io-client/socket.io');

Now you can do as you please with your new glorious websockets.

Liked this content?

Get notified more about React Native!

No Spam! We Promise!

Related Content

Create a useMousePosition Hook with useEffect and useState in React

In this lesson we'll explore how to combine useEffect with useState to create a reusable hook. It will listen add a window listener for mousemove and update state with each movement.

Create a componentDidMount useEffect hook in React

In this lesson we'll explore how to create useEffect call, and listen to the window for mouse movement. We'll learn about cleaning up our effects, and how to pass an empty array into the second argument of our effect to create a componentDidMount like effect. Finally we'll trigger a mount/unmount to show effects getting cleaned up correctly.

Create a useMousePosition Hook with useEffect and useState in React

In this lesson we'll explore how to combine useEffect with useState to create a reusable hook. It will listen add a window listener for mousemove and update state with each movement.

Create a componentDidMount useEffect hook in React

In this lesson we'll explore how to create useEffect call, and listen to the window for mouse movement. We'll learn about cleaning up our effects, and how to pass an empty array into the second argument of our effect to create a componentDidMount like effect. Finally we'll trigger a mount/unmount to show effects getting cleaned up correctly.