I am struggling with an Express/Node/PeerJs application that uses socket.io. It works in localhost but not when pushing to heroku or nodejitsu.
Here is what I have in app.js:
var routes = require('./routes');
var express = require('express');
var path = require('path');
var https = require('https');
var fs = require('fs');
var url = require('url');
var app = express(),
http = require('http'),
server = http.createServer(app),
io = require('socket.io');
app.engine('.html', require('ejs').__express);
app.set('port', process.env.PORT || 5500);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
...
console.log(process.env.port);
app.configure('development', function(){
app.use(express.errorHandler());
});
server.listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
var sockets = io.listen(server);
app.get('/', function(req, res){
res.render('index');
});
//more code
In chatroom.html:
<script type="text/javascript"></script>
<script src="myapp.herokuapp.com:5500/socket.io/socket.io.js"></script>
<script src="http://cdn.peerjs.com/0.3/peer.js"></script>
var socket = io.connect('http://myapp.herokuapp.com:5500/');
var peer = new Peer({key: 'mykey', debug: true});
peer.on('open', function(peer) {
peer_id = peer;
console.log('my peer id is ' + peer_id);
socket.emit('peer', {peer_id: peer_id, chatroom: chatroomString});
console.log('peer id and chatroom string: ', {peer_id: peer_id, chatroom: chatroomString});
});
//rest of code
I've tried changing the path to /socket.io/socket.io.js, adding http, changing ports, and many other silly things. Yet unfortunately it works in localhost but not when I push to both nodejitsu and heroku.
There are some relatively similar questions(that I could find) but none with sufficient answers.
Socket.io Failed to load resource
socket.io: Failed to load resource
Would appreciate the help.
SOLUTION!!!
in my app.js file
var app = express(),
http = require('http'),
server = http.createServer(app),
io = require('socket.io').listen(server);
var sockets = io;
in my chatroom.html file
<script src="/socket.io/socket.io.js"></script>
var socket = io.connect('http://myapp.herokuapp.com/');
You just take the port numbers out of your html file.
Websockets was not supported earlier in Heroku. Now it is supported. You need to set flag for that.
https://devcenter.heroku.com/articles/heroku-labs-websockets
Even after setting the flag, and restarting the app, the sockets are not working, please clarify the exact error that you are getting.
Related
I have code snippet to explain what i am doing and what i want.
var express = require('express');
var http = require('http');
var app = express();
app.use('/', express.static(__dirname + '/static'));
var BinaryServer = require('binaryjs').BinaryServer;
var server = http.createServer(app);
var binaryServer = new BinaryServer({server:server});
var ioServer = http.createServer(app);
var io = require('socket.io').listen(ioServer);
I can run node express and socket.io on same port.
ioServer.listen(8080, function(){
console.log('server running at localhost:8080');
});
Same can be done with node express and binaryServer.
server.listen(8080, function(){
console.log('server running at localhost:8080');
});
But i want to run node express, socket.io and binaryServer on same port express is running (8080 in this case).
Any suggestions ?
You would need to attach both the SocketIO and binaryServer to same http server instance then bring that single instance up.
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var binaryServer = new BinaryServer({ server:server, path: '/binary'});
server.listen(8080, function(){
console.log('http/socket/binary server running at localhost:8080');
});
Set the path so binaryServer doesn't conflict with any of your apps. This path is required in the client connections too.
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 try to create a real time app by socket.io.
Server side:
var express = require('express');
var io = require('socket.io');
var engine = require('ejs-locals');
var app = express()
, server = require('http').createServer(app)
, io = io.listen(server);
app.engine('ejs', engine);
app.set('view engine', 'ejs');
app.get('/', function(req, res){
res.redirect('/login')
});
app.use(express.static(__dirname + '/public'));
app.listen(3001);
io.sockets.on('connection', function (socket) {
console.log('Client connected...');
socket.on('send_login_data', function (data) {
console.log(data);
});
});
Client side:
var socket = io.connect('http://localhost:3001');
socket.on('connect_failed', function(){
console.log('Connection Failed');
});
socket.on('connecting', function () {
console.log('connecting...');
});
socket.on('connect', function () {
console.log('connected!');
});
I caught the next error:
GET http://localhost:3001/socket.io/1/?t=1447539302809 404 (Not Found)
As I understand, it's a handshake error.
How can I fix it?
Thanx.
First off, make absolutely sure there are no errors showing anywhere in case some module isn't installed properly.
Then, make sure you have the same version number of socket.io on client and server and that you have the server-side version installed on the server.
Then, I've seen folks have issue with this before, even when following the instructions on the socket.io web site and it's never been clear exactly what was wrong with that sequence. But, what I know is that this sequence will work:
var express = require('express');
var app = express();
var server = app.listen(3001);
var io = require('socket.io').listen(server);
See related issues: Where is my client side socket.io? and Node + Socket.io Connection issue
I don't know if this is causing the issue or not, but you are attempting to redefine the io variable when it has already been declared in this:
var io = require('socket.io');
var app = express()
, server = require('http').createServer(app)
, io = io.listen(server);
The second reference to io = io.listen(server) is essentially this:
var io = require('socket.io');
var app = express();
var server = require('http').createServer(app);
var io = io.listen(server);
Which is not correct Javascript. Again, may not be causing your issue, but it is not technically proper Javascript.
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
});
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.