Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import Socket.io using import - Nodejs

Not sure why I am getting the error. When I try to do the same thing by using request it works fine.

import express from 'express';
import { createServer } from 'http';
import * as io from 'socket.io';

const app = express();
const server = createServer(app);
const socketio = io(server);

Error

const socketio = io(server);
                 ^

TypeError: io is not a function
like image 902
John John Avatar asked Aug 23 '26 16:08

John John


1 Answers

You should try this instead

import { Server } from "socket.io";
const httpServer = createServer();
const io = new Server(httpServer, {
  // ...
});

Check it out here Socket.io Initialization

like image 102
billobeng Avatar answered Aug 26 '26 05:08

billobeng