Client Side (Socket) - React & React Native
yarn add socket.io-clientSocket Manager
import io, { Socket } from 'socket.io-client';
import { getItemAsync } from "expo-secure-store"; //Or ReactJs with localStorage
import { host } from "./API";
let socket: Socket;
export const getSocket = (): Socket => {
if (!socket && host) {
socket = io(host, {
transports: ['websocket'],
forceNew: true,
upgrade: false,
});
socket.on("connect", () => {
getItemAsync("token").then((token) => {
if (token) {
socketAuth(token);
}
});
})
socket.on("reconnect", () => {
getItemAsync("token").then((token) => {
if (token) {
socketAuth(token);
}
})
})
}
return socket;
};
export const socketAuth = (token: string) => {
socket.emit("auth", token)
}useSocket Hook
Real usage
Last updated