Skip to content

Instantly share code, notes, and snippets.

@louicoder
Created October 14, 2021 10:26
Show Gist options
  • Save louicoder/1a8e239e3249bd6de4cfec37d75ba79f to your computer and use it in GitHub Desktop.
Save louicoder/1a8e239e3249bd6de4cfec37d75ba79f to your computer and use it in GitHub Desktop.
React native websocket connection
import { Text, View, StyleSheet, Alert } from 'react-native';
navigator.__defineGetter__('userAgent', function () {
// you have to import rect native first !!
return 'react-native';
});
import SocketIOClient from 'socket.io-client/dist/socket.io.js';
//
useEffect(() => {
const socket = SocketIOClient('wss://site-name.herokuapp.com/', {
jsonp: false
});
socket.on('connect', () => {
console.log('connected');
socket.emit('hello', 'world');
});
socket.on('connect_error', (err) => {
console.log(err instanceof Error);
console.log(err.message);
});
}, []);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment