简体   繁体   中英

socket.io polling returns Laravel 500 NotFoundHttpException

I'm trying to setup a node.js socket.io server, and it's not working. It seems like it's trying to poll to the node.js server first, but because I'm using Laravel for the majority of my app, that's picking it up and returning a Not FoundHttpException

Node.js server code:

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

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

var redis = require('ioredis');
var Redis = new redis();

Redis.subscribe('live-updates');

Redis.on('message', function(channel, message) {
    message = JSON.parse(message);

    console.log(channel, message);
    io.emit('foo', message.data);
});

io.on('connection', function() {
    console.log('foo');
});

Client code:

var socket = io.connect('http://myapp.app');

socket.on('foo', function(data) {
    console.log(data);
});

All this does is triggers the following HTTP long polling requests in my client:

http://myapp.app/socket.io/?EIO=3&transport=polling&t=1447468676627-0

which responds with:

Server 500 error, #29 Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException in /home/vagrant/myapp/vendor/laravel/framework/src/Illuminate/Routing/RouteCollection.php:161

Well of course it's not found. I'm expecting socket.io to attempt to create a websocket connection - not send long-polling HTTP requests to an undefined Laravel route. What am I doing wrong?

You don't actually need express here, as you are not displaying anything, just simple http server. I think your issue, that the server does not listen.

Also node should listen to different port than 80, otherwise your http server will handle the request and lead to that 500 error.

Check code below.

Server side

var app = require('http').createServer(handler);
var io = require('socket.io')(app);

app.listen(3000);

Client side

var socket = io('http://localhost:3000');

Make sure you run node from CLI

node index.js

And replace index.js with your script name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM