Skip to content

Instantly share code, notes, and snippets.

@williamtran29
Forked from ekryski/app.js
Created February 13, 2019 16:38
Show Gist options
  • Save williamtran29/45d120b7f46bb03e84bc4176d91eb2d9 to your computer and use it in GitHub Desktop.
Save williamtran29/45d120b7f46bb03e84bc4176d91eb2d9 to your computer and use it in GitHub Desktop.
How to use socket.io in React Native
// You need to set `window.navigator` to something in order to use the socket.io
// client. You have to do it like this in order to use the debugger because the
// debugger in React Native runs in a webworker and only has a getter method for
// `window.navigator`.
window.navigator.userAgent = 'ReactNative';
// Need to require instead of import so we can set the user agent first
// This must be below your `window.navigator` hack above
const io = require('socket.io-client/socket.io');
const socket = io('http://chat.feathersjs.com', {
transports: ['websocket'] // you need to explicitly tell it to use websockets
});
socket.on('connect', () => {
console.log('connected!');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment