I have this piece of code on my server
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(3000);
io.sockets.on('connection', function (socket) {
console.log("Socket connected");
});
I just want to create a connection
and on the client -
<script src="public/javascripts/socket.io.js"></script>
<script>
var socket = io.connect('http://127.0.0.1:3000');
</script>
And when I open my browser I get this error in console:
GET http://127.0.0.1:3000/socket.io/1/?t=1404410309733 400 (Bad Request) socket.io.js:1659
XHR finished loading: GET "http://127.0.0.1:3000/socket.io/1/?t=1404410309733".
I've already done this like 10 times and I never get this. Does anyone know how to fix it?
I think an older version of socket-io could be the reason.
I was getting this problem when I was using 0.9.16 version. I upgraded to following and it worked.
<script src="https://cdn.socket.io/socket.io-1.0.6.js"></script>
Related
In my Node.js + Socket.io application, I'm trying to set the ping timeout to 63 seconds (63 seconds of inactivity allowed until the connection between the server and the client(s) is considered closed). Unfortunately, when I run my code below, I get the following error:
var io = socket(server);
^
TypeError: socket is not a function
Below is my code:
var express = require("express");
var socket = require("socket.io")(3000, {
pingTimeout: 63000,
});
var app = express();
var server = app.listen(3000);
app.use(express.static("public"));
var io = socket(server);
just remove this line
var socket = require("socket.io")(3000, { pingTimeout: 63000, });
and try adding this in io variable
var io = require("socket.io")(server, {pingTimeout: 63000, });
How can i get all connected peers if i have simple server:
var port = 9000;
var express = require('express');
var app = express();
var ExpressPeerServer = require('peer').ExpressPeerServer;
app.get('/', function(req,res){
res.send('server OK');
//console.log(ip.address());
});
var server = require('http').createServer(app);
app.use('/peerjs',ExpressPeerServer(server));
server.listen(port,'0.0.0.0');
and client:
var peer = new Peer(
{host:"hostname",
port:9000,path:"/peerjs",
debug:3,
config:servers,
});
i use peerjs lib. Thank in advance!
Peer.js has method listAllPeersĀ on the client side. And you can use it if you add allow_discovery: true parameter in options on the server side.
This should help you
brodcast.emit to send a message to all without socket, and when I do that the node instance crashes and says that socket.brodcast is undefined .
here is my node code:
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.use(express.static('public'));
io.on('connection', function(socket){
socket.on("newChild",childData =>{
var newChildID = mainData.newChild(childData.fatherID,childData.data, childData.type);
socket.emit("newChildID",{ "newId" : newChildID,"old" : childData.localID});
socket.brodcast.emit("newChild",maindata.getDataPoint(newChildID));
});
});
when I emit "newChild" from the client the server crashes and say that socket.brodcast is undefined
The important part is to get socket.brodcast.emit , so do I use the API wrong?
when I googled after it I found this: Send response to all clients except sender (Socket.io)
In this thread I found this example:
socket.on('cursor', function(data) {
socket.broadcast.emit('msg', data);
});
And its seams to be the same as i do.
your code contains a typo for starters...
socket.brodcast.emit("newChild",maindata.getDataPoint(newChildID));
should be
socket.broadcast.emit("newChild",maindata.getDataPoint(newChildID));
I'm experimenting on an app that is running with nodejs, express and socket.io
Server Side:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
io.on('connection', function(socket){
socket.on('send_stuff', function(stuff){
io.emit('log_stuff',newStuff);
});
});
http.listen(54123, function(){
console.log('listening on *:54123');
});
Client Side:
var socket = io.connect('http://example.com:12345');
socket.emit('send_stuff',stuff);
My question is How do I get the details of the client (ip,user-agent,etc.) that executed socket.emit('send_stuff',stuff)?
I want to pass it to the newStuff variable.
socket.on('send_stuff', function(stuff){}); in this line stuff only returns the value that was send by client emit().
Any ideas how to do this?
You can get client's ip and user-agent in connection event.
io.on('connection', function(socket){
console.log("ip: "+socket.request.connection.remoteAddress);
console.log("user-agent: "+socket.request.headers['user-agent']);
})
Documentation can give you some hints http://socket.io/docs/server-api/
This worked for me:
io.on('connection', function(socket) {
var userAgent = socket.handshake.headers["user-agent"];
console.log("User-Agent: " + userAgent);
});
This is first time, I am using socket.io.I stuck at initial stage itself.sorry it's may be simple question.
server side code :
Inside my server.js I written the following code.
var express = require('express')
,io=require('socket.io')
,http = require('http')
var app = express();
server = http.createServer(app);
io = io.listen(server,{ log: false });
Now I trying to make connection inside server.js file,like in the following way.
io.sockets.on('connection', function (socket) {
console.log("This is testing");
io.to(socket.id).emit('notification', 'for your eyes only');
});
client side code :
var socket = io.connect("http://localhost");
socket.on('connect', function () {
console.log("connect")
});
socket.on('notification', function (data) {
console.log(data);
});
I open application in browser, as per my code it suppose to console connect statement but it's not happening.
my server is running on port no :80Where am I did wrong, can anyone help me.
Thanks.
Here is the working code for me in express it may help you.
var express = require('express')
, app = express()
, server = require('http').Server(app)
, io = require('socket.io')(server)
var defaultPort = 6001 ;
server.listen(defaultPort, function() {
console.log('Server Started');
});
io.sockets.once('connection', function(socket) {
return io.sockets.emit('new-data', {
channel: 'stdout',
value: "Your Data Goes Here"
});
socket.on('disconnect', function(){
});
});
On Client Side
<script>
$(function() {
var socket = io.connect('http://localhost'); //if you are trying on server put server url if you are working on local then use localhost
socket.on('new-data', function(data) {
$('#YouDivid').html(data.value);
});
});
</script>