10

I am writing a sample code to handle socket connections in node.js using es6 script, on importing socket.io it raises an error

import {
  PORT
} from './config';

import express from 'express';
import io from 'socket.io';

var app = express();

// respond with "hello world" when a GET request is made to the homepage
app.get('/', function(req, res) {
  res.send('hello world')
});

io.on('connection', function(socket) {
  console.log('a user connected');
});

app.listen(PORT, () => console.log(`Example app listening on port ${PORT}!`));

The error is

/index.js:17 _socket.default.on('connection', function (socket) { ^

TypeError: _socket.default.on is not a function at Object.on (/Users/digvijay.upadhyay/digvijayu/websocket_with_node/src/index.js:15:4) at Module._compile (module.js:643:30) at Module._compile (/Users/digvijay.upadhyay/digvijayu/websocket_with_node/node_modules/pirates/lib/index.js:83:24) at Module._extensions..js (module.js:654:10) at Object.newLoader [as .js] (/Users/digvijay.upadhyay/digvijayu/websocket_with_node/node_modules/pirates/lib/index.js:88:7) at Module.load (module.js:556:32) at tryModuleLoad (module.js:499:12) at Function.Module._load (module.js:491:3) at Function.Module.runMain (module.js:684:10) at Object. (/Users/digvijay.upadhyay/digvijayu/websocket_with_node/node_modules/@babel/node/lib/_babel-node.js:224:23) [nodemon] app crashed - waiting for file changes before starting... Successfully compiled 2 files with Babel. Successfully compiled 2 files with Babel.

Digvijay Upadhyay
  • 709
  • 1
  • 11
  • 25
  • 1
    Call `io()` after importing. Something like `socket = io()` – tbking Oct 22 '18 at 19:55
  • for server side:--> import * as socket_io from 'socket.io' ; const socket = socket_io.default(http) – yehonatan yehezkel Oct 22 '20 at 14:36
  • 1
    This question should be re-opened, it is different from the question that it is marked as a duplicate of. – Foobar Nov 19 '20 at 04:50
  • 5
    import {Server} from 'socket.io" const io = new Server(app); – Eduard Jacko Nov 24 '20 at 19:37
  • Thank you @EduardJacko - watch for the typo though. You have one single quote and one double quote around 'socket.io" :P – Charles Holbrow Nov 24 '20 at 22:35
  • 1
    Just to clarify @EduardJacko answer, the `app` variable passed to the `Server` class should be the result of calling the `Server()` method of the `http` module with the express app. This is an example changing the name of the `app` variable for `server`: `const app = express(); const server = http.Server(app); const io = new Server(server);` and also: `server.listen(PORT, () => console.log("Listening on port " + PORT));` – Ramon Carceles Jan 06 '21 at 12:55
  • 1
    `import { Server as socketIO } from 'socket.io'` – Uahnbu Tran Feb 03 '21 at 17:52

1 Answers1

-8

You need to invoke function require('socket.io')() and pass there an express instance.

Please look at added code example:

const express = require('express');
const http = require('http');
const socketIO = require('socket.io');

const app = express();
const server = http.createServer(app);
// this line \/
const io = socketIO(server);
// this line /\


io.on('connection', (socket) => {
    //...
});

server.listen(port, () => {
    //...
});
mat.mik
  • 97
  • 1
  • 8
  • 5
    I have read the solution using `require` but I was wondering if it was possible to import using es6 `import` – Digvijay Upadhyay Oct 22 '18 at 19:47
  • 1
    Yes, it is possible - but still you need to invoke ``socketIO()`` after import – mat.mik Oct 23 '18 at 15:19
  • so whats the answer? `io(http)` not work – mandaputtra Apr 30 '19 at 03:26
  • @mat.mik socketIO() is not a function so what do you mean by that? – Nditah Aug 05 '19 at 09:12
  • 3
    OP wrote its code snippet using ECMAScript Modules. The solution you provided was written using CommonJS. Not only you didn't not answer the question, but you didn't even started with the code provided by the OP. What question are you answering? – Amin NAIRI Dec 19 '20 at 11:57