Am using socket-io to do a delivery app, but i dont know why is generating me two connections, if only have a cellphone connected someone can help with that ?
I need to export the io to do emit and other thins in the routes petitions, type of when a new order is created, o a order is accepted.
Mi socketio.js code is this
import { Server as WebSocketServer } from "socket.io";
import http from "http";
import app from "../app";
const httpServer = http.createServer(app);
const io = new WebSocketServer(httpServer);
export {
httpServer,
io,
}
Mi index js is this.
import { httpServer, io } from "./middlewares/socketio";
import "./database";
require("dotenv").config();
var admin = require("firebase-admin");
var serviceAccount = require("./deliveram-test-firebase-adminsdk-vqinw-368ea4a624.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
});
io.on("connection", function (socket) {
console.log("New Connection: ", socket.id);
});
httpServer.listen(process.env.PORT || 3000, () => {
console.log("Server is running on port: " + process.env.PORT || 3000);
});

Am using this context to manage the petitions
import React, {createContext, useState, useContext, useEffect} from
'react';
import {showError} from '../utils/helperFunctions';
import {io} from 'socket.io-client';
import {API_URL} from '../config/urls';
const socket = io(API_URL);
const SocketContext = createContext();
const SocketProvider = ({children}) => {
const [newOrder, setNewOrder] = useState();
const [deleteOrder, setDeleteOrder] = useState();
const [newUbication, setNewUbication] = useState();
const [orderUpdated, setOrderUpdated] = useState();
const [orderAccepted, setOrderAccepted] = useState();
const [socketId, setSocketId] = useState();
socket.on('connect', () => {
setSocketId(socket.id);
});
socket.on('orderSaved', data => {
setNewOrder(data ? data : null);
});
socket.on('orderDeleted', data => {
setDeleteOrder(data ? data : null);
});
socket.on('updateUbication', data => {
setNewUbication(data ? data : null);
});
socket.on('orderAccepted', data => {
setOrderAccepted(data ? data : null);
});
return (
//This component will be used to encapsulate the whole App,
//so all components will have access to the Context
<SocketContext.Provider
value={{
newOrder,
deleteOrder,
newUbication,
orderAccepted,
socketId,
}}>
{children}
</SocketContext.Provider>
);
};
//A simple hooks to facilitate the access to the AuthContext
// and permit components to subscribe to AuthContext updates
function useSocketIO() {
const context = useContext(SocketContext);
if (!context) {
throw new Error('useSocketIO must be used within an SocketProvider');
}
return context;
}
export {useSocketIO, SocketContext, SocketProvider};