3

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
John John
  • 1,305
  • 2
  • 16
  • 37

4 Answers4

13

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

billobeng
  • 156
  • 6
4

Here is the answer

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

const app = express(); 
const server = createServer(app); 
const socketio = new Server(server);
John John
  • 1,305
  • 2
  • 16
  • 37
3

Here's a better and more professional 2021 approach for the use of Socket.io with Express and ES modules initialization:

Here you can see the import and initialization inside the file Socket.js:

    import { Server } from 'socket.io';
    
    class SocketServer {
      constructor(server) {
        const io = new Server(server);
    
        this.socket = io.of('/session').on('connection', (socket) => {
          const chatApiInstance = JSON.parse(
            socket.handshake.query.chatApiInstance
          );
    
          if (chatApiInstance) {
            socket.join(chatApiInstance);
          }
        });
      }
    
      onNewMessage(chatApiInstance, message) {
      
        this.socket.to(chatApiInstance).emit('newMessage', JSON.stringify(message));
      }
    
      onStatusUpdate(chatApiInstance, status) {

        this.socket.to(chatApiInstance).emit('statusChange', status);
      }
    }
    
    export default SocketServer;

And then in your Server.js, you should import Socket.js like this:

import app from './app';
import SocketServer from './socket';

const server = app.listen(process.env.PORT || 3333, '0.0.0.0');

const Socket = new SocketServer(server);

export default Socket;

Then you need a Controller to handle this requisition, like this one:

import Socket from '../../server';

class SessionController {
  async create(request, response) {
    const { instanceId, messages, status } = request.body;

    if (messages && messages.length) {
      Socket.onNewMessage(instanceId, messages[0]);
    } else if (status) {
      Socket.onStatusUpdate(instanceId, status);
    }

    return response.send('OK');
  }
}

export default new SessionController();

Lastly, don't forget to add the route, like this:

import { Router } from 'express';

import SessionController from '../Controllers/SessionController';

const sessionRouter = Router();

sessionRouter.post('/', SessionController.create);

export default sessionRouter;


Check more here for the initialization of the v3 of Socket.io: https://socket.io/docs/v3/server-initialization/index.html

Frederiko Ribeiro
  • 1,844
  • 1
  • 18
  • 30
1

Try this:

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

const app = express(); 
const server = createServer(app); 
const socketio = new io.Server(server);
Megh Agarwal
  • 216
  • 1
  • 7