Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

socket.io can't get it to work

EDIT 1:

Ok, problem with the server solved. This is the full server code integrated into my project.

var http =              require('http');
var express =           require('express');
var requestHandler =    require(__dirname + '/app_modules/request-handler.js');
var app =               express();

app.configure(function(){
    app.use(express.static(__dirname + '/html'));
    app.use(express.bodyParser());
});

var httpServer = http.createServer(app);
httpServer.listen(80);

var io = require('socket.io').listen(httpServer);

io.on('connection', function(socket){
    socket.on('event', function(data){

    });
    socket.on('disconnect', function(){

    });
});

The next issue is including the source .js in my static html page. This throws an error:

<script src="/socket.io/socket.io"></script>

Resource interpreted as Script but transferred with MIME type text/plain: "http://localhost/socket.io/socket.io". localhost/:206
Uncaught SyntaxError: Unexpected identifier socket.io:1
Uncaught ReferenceError: io is not defined 

ORIGINAL PROBLEM:

I'm trying to get this sucker working on a test server. This is 1:1, straight from the socket.io docs on github.

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

io.on('connection', function(socket){
    socket.on('event', function(data){

    });
    socket.on('disconnect', function(){

    });
});

server.listen(3000);

Here is what I'm getting:

[INFO] 19:37:40 Restarting
[ERROR] 19:37:40 TypeError
TypeError: object is not a function
    at Object.<anonymous> (/Users/me/work/github/node-forever-gui/server/socket_test.js:3:30)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Object..js (/usr/local/lib/node_modules/node-dev/lib/hook.js:52:17)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/usr/local/lib/node_modules/node-dev/lib/wrap.js:47:1)
    at Module._compile (module.js:456:26)

All modules are installed through npm. Please help me figure this out!

like image 631
Nikolay Dyankov Avatar asked Dec 16 '22 05:12

Nikolay Dyankov


2 Answers

The is exactly what is stated, io is not a function and you can't pass a HTTP object to it. Use the listen method instead to attach Socket.IO to a HTTP instance.

var io = require('socket.io').listen(server);

For your second error, the file is a script, and has an extension. Add the .js to the reference.

<script src="/socket.io/socket.io.js"></script>
like image 129
hexacyanide Avatar answered Dec 29 '22 20:12

hexacyanide


some people have this problem because the import in an es6 env was written wrong

My code when it was buggy

import { socketIo } from 'socket.io';

// start server on port
const server = app.server.listen(process.env.PORT || config.port, () => {
    console.log(` ====== Listening on ${app.server.address().port} ====== `.bgWhite.black.bold);
});

// socketIo.io
const io = socketIo(server);
io.on('connection', (socket) => {
    console.log(` ====== Socket Connected => `.bgWhite.black.bold, socket.id);
});

UnhandledPromiseRejectionWarning: TypeError: socket_io_1.socketIo is not a function

fix was simple

import * as socketIo from 'socket.io';

here is an example https://github.com/luixaviles/socket-io-typescript-chat/blob/master/server/src/chat-server.ts

like image 22
Omar Avatar answered Dec 29 '22 19:12

Omar