Dean Collins
Dean Collins

Reputation: 243

Laravel 5 - Homestead - Redis - NodeJs - Socket IO not playing nicely

I am attempting to get some real time notifications into my Laravel 5 app. Building locally on a vagrant box with Homestead.

I can not work out what the issue is with the following set up.

My server.js is as follows ...

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

server.listen(3000);

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

  var redisClient = redis.createClient();
  redisClient.subscribe('message');

  redisClient.on("message", function(channel, message) {
    socket.emit(channel, message);
  });

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

});

Then my client code is to subscribe:

<script src="https://cdn.socket.io/socket.io-1.3.4.js"></script>

<script>
var socket = io.connect('http://127.0.0.1:3000');
socket.on('message', function (data) {
   alert(data);
});
</script>

Firebug keeps returning:

GET http://127.0.0.1/socket.io/?EIO=3&transport=polling&t=1430825965165-1 Aborted

Chrome tools shows me the following:

http://127.0.0.1:3000/socket.io/?EIO=3&transport=polling&t=1430826106108-3 net::ERR_CONNECTION_REFUSED

Pretty new to NodeJs and very confused. Spent a good 4 hours now trying to work it out.

Any pointers?

many thanks, Dean.

Upvotes: 2

Views: 1351

Answers (2)

Jinal Patel
Jinal Patel

Reputation: 36

Use your project's virtual host name for connect with socket. It works for me. Edit this in your client code. var socket = io.connect('http://' + location.host + ':3000');

Upvotes: 0

Gonzalo Artur
Gonzalo Artur

Reputation: 35

First make sure that you have installed all dependencies of your server.js file, if you are working with Homestead, execute the following command in your root project directory:

npm install express redis socket.io --save

Wait until installation finish and start the server with:

node server.js

Upvotes: 0

Related Questions