Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

279
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!