1
votes

I am studying Full-Stack Redux on the following page: http://teropa.info/blog/2015/09/10/full-stack-redux-tutorial.html#introducing-the-redux-store

After created new server using socket-io in server.js and run app using babel-node, I got the following error:

/home/ubuntu/workspace/src/server.js:15

var io = new _socket2.default().attach(8090);
         ^

TypeError: _socket2.default is not a function

at startServer (server.js:4:16)
at Object.<anonymous> (index.js:5:1)
at Module._compile (module.js:409:26)
at loader (/home/ubuntu/workspace/node_modules/babel-cli/node_modules/babel-register/lib/node.js:126:5)
at Object.require.extensions.(anonymous function) [as .js] (/home/ubuntu/workspace/node_modules/babel-cli/node_modules/babel-register/lib/node.js:136:7)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at /home/ubuntu/workspace/node_modules/babel-cli/lib/_babel-node.js:161:27
at Object.<anonymous> (/home/ubuntu/workspace/node_modules/babel-cli/lib/_babel-node.js:162:7)

This is the code of server.js

import Server from 'socket.io';

export default function startServer() {
    const io = new Server().attach(8090);
};

This is the code of index.js

import makeStore from './src/store';
import startServer from './src/server';

export const store = makeStore();
startServer();

and this is package.json file:

{
  "name": "chat-example",
  "version": "0.0.0",
  "description": "A chat example to showcase how to use `socket.io` with a static `express` server with `async` for control flow.",
  "main": "server.js",
  "repository": "",
  "author": "Mostafa Eweda <mo.eweda@gmail.com>",
  "scripts": {
    "start": "babel-node index",
    "test": "mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive",
    "test:watch": "npm run test -- --watch"
  },
  "devDependencies": {
    "babel-cli": "^6.6.5",
    "babel-core": "^6.7.2",
    "babel-preset-es2015": "^6.6.0",
    "chai": "^3.5.0",
    "chai-immutable": "^1.5.3",
    "mocha": "^2.4.5"
  },
  "babel": {
    "presets": [
      "es2015"
    ]
  },
  "dependencies": {
    "async": "~0.2.8",
    "express": "~3.2.4",
    "immutable": "^3.7.6",
    "redux": "^3.3.1",
    "socket.io": "^0.9.17"
  }

I did exactly the same as what was written on the page. Please give me suggestions on this case. Thanks

3

3 Answers

1
votes

I found the solution for this problem, I answer here in case somebody need it. I believe socket.io no longer uses attach function. Instead they use the following code to create socket server:

const io = Server(8090);
0
votes

You should change socket.io version to at least 1.4.5: it will be fine!

Please refer to the original source code: https://github.com/teropa/redux-voting-server/blob/master/package.json

Good luck!

0
votes

In your server.js

import Server from "socket.io";

const startServer = port => {
  console.log(`Listening on ${port}`);
  const io = new Server().attach(port);
};
export default startServer;

In your index.js

import makeStore from "./src/store";
import startServer from "./src/server";

export const store = makeStore();
startServer(8090);