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("");
const cors = require("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.on("join_room", (data) => {


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

Client-side code:

import "./App.css";
import io from "";
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">
        placeholder="Room Number..."
        onChange={(event) => {
      <button onClick={joinRoom}> Join Room</button>
        onChange={(event) => {
      <button onClick={sendMessage}> Send Message</button>
      <h1> Message:</h1>

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/ 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?

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?

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.