socket.io polling returns Laravel 500 NotFoundHttpException - php

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.

Related

Cannot connect client to server using socket

Motivation:
I want to update my front-end directly from the server.
I can't connect the client to the server. (Error on client)
Socket.io v4
Node.JS server runs on port 3000
Client on port 80
Server side
var express = require('express');
const { Server } = require("socket.io")
const http = require('http');
const Sockets = require('./sockets');
var app = express();
const miserver = http.createServer(app);
app.use(express.static(__dirname + '/public'));
const httpserver = miserver.listen(3000);
console.log("Servidor corriendo en el puerto 3000");
//incluir socket IO
const el_io = new Server(httpserver, {
cors: {
origin: "http://localhost:80",
methods: ['POST', 'GET'],
credentials: true
}
});
Sockets(el_io);
Client side
<script src="../socket.io.js"></script>
<script>
var socketIO = io('ws://localhost:3000/socket.io/?EIO=3&transport=websocket');
</script>
the socket run at port 80
Nothing in this code you show is running on port 80. You show the creation of ONE http server that is running on port 3000.
Also, this:
io('ws://localhost:3000/socket.io/?EIO=3&transport=websocket');
is wrong. The path and parameters are things that socket.io adds to the URL itself. You should just be doing this:
const socketIO = io('http://localhost:3000');
and, if you want to avoid CORs issues, you can tell socket.io to go directly to a webSocket like this:
const socketIO = io('http://localhost:3000', {transports: ['websocket']});

Use socket.io in codeigniter

I want to use socket.io in codeigniter and I have followed all the process like install express and socket.io in root and I also have created the server.js file bellow
server.js
var socket = require('socket.io');
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = socket.listen(server);
var port = process.env.PORT || 3000;
server.listen(port, function() {
console.log('Server listening at port %d', port);
});
io.on('connection', function(socket) {
socket.on('new_message', function(data) {
io.sockets.emit('new_message', {
message: data.message,
date: data.date,
msgcount: data.msgcount
});
});
});
When I run this server.js in terminal, it outputs 'Server listening at port 3000',
but when I request localhost:3000 in browser, it shows Cannot GET /. I do not understand this error.
You are adressing http request to express router which is not set in your code. Socket.io is sending xhr or socket requests and does not handle http requests. Either use online services like Socket echo or raise router which will receive your browser request and echo it it via socket to your socket.io instance.

integrating nodejs, socket.io and express on laravel application on production environment

We have existing laravel application running on VPS provider on ubuntu with apache, we have to implement real-time features through node app (socket.io and express) seems fine in local environment but when i try it to production it doesnt working at all, my question is, what is the proper configuration i will be made in order to run my node app like $ node server.js
snippet code(server.js).
var socket = require('socket.io');
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = socket.listen(server);
var port = process.env.PORT || 3001;
server.listen(port, function () {
console.log('Server listening at port %d', port);
});
io.on('connection', function(socket) {
code here....
});
snippet code(client-side(js-file)).
var socket = io();
ERROR
GET https://hostname/socket.io/?EIO=3&transport=polling&t=1480917572919-5 404 (Not Found)
snippet code(client-side(js-file)).
var socket = io() ;
to
var socket = io.connect('http://localhost:3001');

Javascript connect to websocket

Following up form my last question -
var socket;
if ("WebSocket" in window)
{
alert("WebSocket is supported by your Browser!");
// Let us open a web socket
socket = new WebSocket("ws://localhost:10001");
}
socket.onopen() = function(){
alert("Connection Opened");
}
socket.onmessage() = function(msg){
alert(msg);
}
I can connect to the server with telnet but I can't seem to connect using Javascript, why is this?
Because WebSocket is not a normal, general-purpose socket. It requires the server on the remote end to conform to a very specific handshake defined by the WebSocket protocol. If your server does not implement this protocol, WebSocket cannot connect to it.
Additionally, as Rocket points out, your code is currently attempting to call socket.onopen() and assign a value to the function call. Lose the parentheses.

Trigger a nodeJS socket.io server to broadcast via port 80

Realtime Update Mechanism : user writes -> php save it in mysql db -> php send info to nodeJS -> nodeJS send the change all subscribers -> others can notice it in realtime.
The Socket.io server works well and runs on port 8080. I have node http server running on port 80. How can I get a trigger message on the http server and send the message to all socket.io clients?
var io = require('socket.io').listen(8080);
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Message Broadcast\n');
console.log("Message Sent");
//DOES NOT SEEM TO WORK, error
io.broadcast("Messages");
//No error but no messages
io.emit("Message");
}).listen(80, "0.0.0.0");
/**
Other socket.io code which works well..
....
In Socket.IO 0.7 you should use the following instead to send to all connected sockets without a specific socket reference:
io.sockets.send("Messages")
Note that this is different to broadcast since that would send to all sockets except the socket that initiated the connection - of course you require a sending socket reference which is why your current code wouldn't suffice.
Quote from the wiki page: Socket.IO/Wiki
If you want to send a message to everyone you can reference io.sockets:
io.sockets.send('message');
io.sockets.emit('event');

Categories