Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect2 and Socket.io

I'm trying to get connect and socket.io to work together nicely and simply. I have the following code on server side:

var connect = require('connect'),
    io = require('socket.io');

var app = connect().use(connect.logger('dev'));
var sio = io.listen(app);

app.listen(8000);

when i open http://localhost:8000/socket.io/socket.io.js i'm get error:

Cannot GET /socket.io/socket.io.js

And Socket.IO not work, i'm trying copy file and load from another location, but socket.io requests do not reach the server

like image 649
skyman Avatar asked Apr 17 '12 11:04

skyman


People also ask

Is Socket.IO obsolete?

This package has been deprecated.

How do I connect Socket.IO to front end?

You need to pass your server there, not the http module and you need to assign the result to an io variable. So, after you've defined and initialized your server variable, you would do: const io = require("socket.io")(server);

How do I connect Socket.IO to another server?

const myemail = [email protected]; const device_id = 12345; io = require('socket. io-client'); var socket = io. connect('https://server.net:3000',{secure: true, rejectUnauthorized: false}); function doStuff(){ //Listener socket. on('connect', function(){ try { console.


1 Answers

SOLUTION

if anyone comes to this issue, you need to wrap the connect/express app in a node http.Server. The app.listen() method is a convenience method for this and returns the server:

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

or the following is equivalent:

var io = require('socket.io');
var http = require('http');
var app = connect();
var server = http.createServer(app);
server.listen(3000);
io.listen(server);
like image 154
skyman Avatar answered Sep 20 '22 05:09

skyman