Socket.io 404 poll in Laravel

I am trying to implement a chat application with Socket.io into my Laravel application. The chat app works fine on it, but I'm having trouble getting it to work in Laravel.

I am trying to serve Laravel on port 8000 and chat server on 8000. I am using Express 4.8.0 and Socket.io 1.0.6, Node 0.10.29 and nodemon for testing.

//server.js:

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

http.listen(8000, function () {
 console.log('listening on *:8000');
});

app.use('/', express.static(__dirname + '/public'));
app.get("/*", function (req, res){
 res.sendFile(__dirname + "/index.php");
});

      

//client.js:

var socket            = io.connect('http://localhost:8000');

      

// html - dependencies, I've tried it all:

<script src="//cdn.socket.io/socket.io-1.0.0.js"></script>
{{ HTML::script('/socket.io/socket.io.js') }}
<script src="http://localhost:8000/socket.io/socket.io.js" ></script>
<script src="{{asset('/socket.io/socket.io.js')}}"></script>

      

and then for client side (native code)

{{ HTML::script('js/client.js') }}

      

The CDN Socket.io version constantly provides logs like this:

"GET http://localhost:8000/socket.io/?EIO=2&transport=polling&t=1407425555977-15 404 (Not Found)".

      

The others just give js file not found log:

"GET http://localhost:8000/socket.io/socket.io.js 404 (Not Found)"

      

// folder structure:

/ public

/ Js

     
  

client.js

  
  

/ node_modules

     

server.js

Can anyone see what I can do to make it work?

CHANGE //server.js

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

var app     = express();
var server  = http.createServer(app);

var io      = socket.listen(server);

io.on('connection', function (socket) {
 console.log("Connected server");
}

server.listen(8000);

      

//client.js

var socket;
$(document).ready(function () {
 socket            = io.connect('http://localhost:8000');
});

      

// When I use the global "socket" object in the log, it says:

connected: false
disconnected: true

      

+3


source to share


1 answer


This is because you configured it incorrectly. I had the same exact problem as you (same errors and underlying code). You need to do npm install socket.io --save

in the base directory of your page (same as where the index.php file is). Then you should do the same for express ( npm install express --save

). You also need to change the server code. Change the io creation from:

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

      

To:

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

var app = express();
var server = http.createServer(app);

var io = socket.listen(server);

      



Then remove the app.use

and app.get

as they are no longer needed for how this will be done. Then add server.listen(8000);

to the end of server.js. For dependencies, use: <script src="//cdn.socket.io/socket.io-1.0.0.js"></script>

. Then, to start your server, go to it in the terminal and type node server.js

. Then just connect to it with your client. Also, for events on the server use:

io.on('connection', function (client) {
    client.on('someEvent', function(someVariables){
        //Do something with someVariables when the client emits 'someEvent'
        io.emit('anEventToClients', someData);
    });
    client.on('anotherEvent', function(someMoreVariables){
        //Do more things with someMoreVariables when the client emits 'anotherEvent'
        io.emit('anotherEventToClients', someMoreData);
    });
});

      

And in your client code:

socket.emit('someEvent', variables);
socket.on('anEventToClients', function(something){
    //Code when anEventToClient is emitted from the server
});

      

+2


source







All Articles