Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

280
Vistas
Socket IO Generating multiple connections

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);
});

enter image description here

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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};
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda