Getting connection refused error in socket io app

Server-side code:

const express = require("express");
const app = express();
const http = require("http");
const { Server } = require("socket.io");
const cors = require("cors");

app.use(cors());

const server = http.createServer(app);

const io = new Server(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
}
});

io.on("connection", (socket) => {
console.log(`User Connected: ${socket.id}`);

socket.on("join_room", (data) => {
socket.join(data);
});

});

server.listen(8080, () => {
console.log("SERVER IS RUNNING");
});


Client-side code:

import "./App.css";
import io from "socket.io-client";
import { useEffect, useState } from "react";

const socket = io.connect(`http://localhost:8080`);

function App() {
  //Room State
  const [room, setRoom] = useState("");

  const joinRoom = () => {
    if (room !== "") {
      socket.emit("join_room", room);
    }
  };

  return (
    <div className="App">
      <input
        placeholder="Room Number..."
        onChange={(event) => {
          setRoom(event.target.value);
        }}
      />
      <button onClick={joinRoom}> Join Room</button>
      <input
        placeholder="Message..."
        onChange={(event) => {
          setMessage(event.target.value);
        }}
      />
      <button onClick={sendMessage}> Send Message</button>
      <h1> Message:</h1>
      {messageReceived}
    </div>
  );
}

export default App;

But getting the following error after hosting it on cloudflare pages. Same thing works fine in local.

type or paste code here

[Error](polling.js:311          GET`http://localhost:8080/socket.io/?EIO=4&transport=polling&t=OKjwWGP net::ERR_CONNECTION_REFUSED)

Can you please help me?

type or paste code here
````Preformatted text`

Hi there,

Where are you running this server code? It’s using express, which isn’t going to work with Workers, and therefore Pages. Are you running this externally?

Pages Functions offers similar routing functionality with file-based routes which you can read more about at Routing · Cloudflare Pages docs.

Otherwise, please provide a full reproducible example so we can assist you further. A GitHub repo or something we can clone and deploy to reproduce the issue would be ideal.

1 Like
Please have a look at this git repository. Where can I host the server in cloudflare?
https://github.com/sudarshanreddyc/socketio-new

You can’t unfortunately. You’ll need to find somewhere else to host the server such as a generic Node.js host, or refactor it with the Workers environment in mind.

May I know how to create or host any server on Cloudflare and use it to listen for socket io?
Thanks

I don’t think you can use localhost since that’s a local hostname which would point to the machine that the server understands as localhost. As mentioned above, you should get an external vps / server to run your nodejs express (inclusive of socket io endpoint) and make sure it points to the IP address or hostname of that external server

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.