I am trying to implement a chat app with Socket.io
in to my Laravel app. The chat app works fine on it's own,
but I am having problems to make it work in Laravel.
I try to serve Laravel on port 8000 and the chat server on 8000.
I use 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 tried all these:
<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 the client side (own code)
{{ HTML::script('js/client.js') }}
The CDN version of Socket.io gives constantly these kinds of logs:
"GET http://localhost:8000/socket.io/?EIO=2&transport=polling&t=1407425555977-15 404 (Not Found)".
The others ones just gives a 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?
EDIT
//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 typ the global "socket" object in the log it says:
connected: false
disconnected: true
This is because you have set it up incorrectly. I had the same exact problem you did (same errors and basic code layout). You need to do npm install socket.io --save while in the base directory of your page (the same as where your index.php file is located). Then you have to do the same for express (npm install express --save). You also have to change your server code. Change the creation of io 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 is going to be done. Then add server.listen(8000); at the end of the server.js. For dependencies, use: <script src="//cdn.socket.io/socket.io-1.0.0.js"></script>. Then, to run your server, go to it in terminal and type node server.js. Then just connect to it with your client. Also, for events, in 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
});
Related
I have been trying to get my client connected with my server,but chrome always print that
GET http://localhost:30653/socket.io/socket.io.js 404 (Not Found)
I don't know where's the problem...
the express version is "4.13.4" and socket.io version is "1.4.5"
And here is my code:
app.js
var express = require('express');
var hbs = require('hbs');
var app=express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
io.on('connection',function(socket){
console.log("connected");
socket.emit('open');
});
app.set('port', process.env.PORT || 30653);
app.set('view engine','html');
app.engine('html',hbs.__express);
app.use(express.static('public'));
app.get('/',function(req,res){
res.render('chatroom');
});
app.listen(app.get('port'),function(){
console.log('this server is listening on port:'+app.get('port'));
});
client:
$(function(){
var socket = io.connect('http://localhost:30653');
socket.on('open',function(){
console.log("open")
});
socket.on('system',function(json){
console.log("system");
});
});
any help is welcome!I'll be very appreciate it!
I think your app.listen(...) needs to be server.listen(...) because of the way you are creating your server as illustrated here: http://socket.io/docs/#using-with-express-3/4. The way you are doing it, socket.io is not hooked to the right server and thus is not serving the socket.io.js file for you.
You can do app.listen(), but only if you follow a different initialization procedure here: http://socket.io/docs/#using-with-the-express-framework
You need use the below line of code in app.js after the line app.use(express.static('public'));
app.use("/lib", express.static(path.join(__dirname,'node_modules')));
and then import in your client as below
<script src='/lib/socket.io/socket.io.js' type='text/javascript'></script>
I'm pretty new to NodeJS and trying to get Socket.IO running with an Express application. I've tried to stick to the docs and tutorials as close as possible, but in the end, socket.io.js cannot be found on client side.
The server is being started as follows:
var app = require('../app');
app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() { debug('...') } );
Routing/serving pages:
var express = require('express');
var router = express.Router();
router.get('/', function (req, res) {
res.render('index', { title: 'Express' });
});
Including socket.io:
var express = require('express');
var http = require('http').Server(express);
var io = require('socket.io')(http);
And in the HTML file, for clients:
<script src="/socket.io/socket.io.js" type="text/javascript">
But when loading the page, this JS-file is never found. I suspect that including socket.io via that previously created http server is somehow wrong... Or am I missing something else?
Update
I just found out that the snipped that includes socket.io is executed before the server is started (first snippet). They're in different files and the server-start one (named www.js) is configured as "start" script in package.json, the other one (websockets.js) required indirectly by that.
If I put
var io = require('socket.io')(server);
at the end of www.js, everything works just fine. But is there a way to do it from websockets.js, which is loaded before, e.g. by a server start callback function?
Try specifying the full path, like this
<script src="http://localhost:8000/socket.io/socket.io.js"></script>
And connect to your server from client side:
var socket = io.connect('http://localhost:8000/');
also check if socket.io module is available in your node_modules directory.
I am a ios developer, the Objc-Socket.io library is still support socket.io 0.9.x
I use Express 4.0 with my nodejs server:
server.js
var express = require('express')();
var io = require('socket.io');
io.listen(express);
But I run it in terminal, it throw me an error:
% node test
Socket.IO's `listen()` method expects an `http.Server` instance
as its first parameter. Are you migrating from Express 2.x to 3.x?
If so, check out the "Socket.IO compatibility" section at:
https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x
This code should do the trick:
var express = require('express');
var http = require('http');
var app = express();
var server = http.createServer(app).listen(1337);
var io = require('socket.io').listen(server);
io.on('connection', function(socket) {
//emit and listen messages
});
app.get('/', function(req, res) {
//do something
});
In my application i need to connect two socket.io node applications.Using socket.io-client we can do like this.But i don't know how socket.io-client works and where to include that.
First Node Application
var express = require('express')
, http = require('http');
var app = express();
app.use(function (req, res) {
app.use(express.static(__dirname + '/public'));
});
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(3000);
io.sockets.on('connection',function(socket){
socket.on('eventFiredInClient',function(data){
socket.emit('secondNodeAppln',data);// i need to get this event in my 2nd node application how can i do this by using socket.io-client
});
});
Second Node Application
var express=require('express');
var http=require('http');
var app=express();
app.configure(function(){
app.use(express.static(__dirname + '/public'));
});
var server = http.createServer(app);
var serverAddress = '127.0.0.1';
var serverPort = 3000; //first node appln port
var clientio = require('socket.io-client');
var socket = clientio.connect(serverAddress , { port: serverPort });
socket.on('connect', function(){
console.log('connected');
});
socket.on('disconnect', function(){
console.log('disconnected');
});
var io = require('socket.io').listen(server);
server.listen(6509);
//here i need to get the 'secondNodeAppln' event raised in first node application.How can i do this.
You need to create a socket.io client in your first app:
var io = require('socket.io').listen(server); // this is the socket.io server
var clientio = require('socket.io-client'); // this is the socket.io client
var client = clientio.connect(...); // connect to second app
io.sockets.on('connection',function(socket) {
socket.on('eventFiredInClient',function(data) {
client.emit('secondNodeAppln', data); // send it to your second app
});
});
And in your second app, just listen for those events:
io.sockets.on('connection', function (socket) {
socket.on('secondNodeAppln', function(data) {
...
});
});
There's a bit of a race condition because the code above doesn't wait for a connect event on the client socket before passing events to it.
EDIT see this gist for a standalone demo. Save the three files to a directory, start the servers:
node serverserver &
node clientserver
And open http://localhost:3012 in your browser.
I'm making a web-app that makes use of nodejs, mongodb, socket.io, express and mongoose.
I can start my server and correctly get the wanted html file in my browser when browsing to my localhost.
The problem I have is getting my socket.io to work.
On my server side everything works fine : I get " info - socket.io started " in my terminal.
But when surfing to my browser I get this in my browser console
Failed to load resource: the server responded with a status of 404 (Not Found)
Uncaught ReferenceError: io is not defined
This is how i connect to socket.io.js
<script src="/socket.io/socket.io.js"></script>
and my map structure looks like this:
map
-app.js
-public
--index.html
-node_modules
--socket.io
--mongodb
--express
--jade
--mongoose
Does anyone knows what the mistake is I've made?
(it's actually the same problem as here: Node.js socket.io.js not found or io not defined )
Thanks in advance!
EDIT:
My code on the server side is this:
var app= require('express').createServer();
var io = require('socket.io').listen(app);
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/db');
app.listen(3030);
console.log("server started");
app.get('/',function(req,res){
res.sendfile(__dirname + '/public/index.html');
});
io.sockets.on('connection',function(socket){
console.log("connection made");
});
The first log, gets logged in the terminal ("server started"), but the second one ("connection made") doesn't get logged. So the connection isn't made.
I thought that was because of the wrong "set up" in my client side.
Check out the express migration guide 2->3
https://github.com/visionmedia/express/wiki/Migrating-from-2.x-to-3.x
Something like this should work
var express = require('express');
var app = express();
var http = require('http');
var server = http.createServer(app);
var io = require('socket.io').listen(server);
var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/db');
server.listen(3030);
console.log("server started");
app.get('/',function(req,res){
res.sendfile(__dirname + '/public/index.html');
});
io.sockets.on('connection',function(socket){
console.log("connection made");
});
var app = express();
app.set('port', process.env.PORT || 3000);
...
var server = http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
var io = socket.listen(server);
io.sockets.on('connection', function () {
console.log('hello world im a hot socket');
});