Socket IO using long polling instead of sockets - node.js

I'm just getting started again with Node and Socket IO. I'm using socket.io v1.3.7.
I can load the socket IO client but it is long polling instead of using sockets.
The last time I used socketIO it worked fine. I think that was the 0.9.x branch.
http://sockettest.dev:3000/socket.io/?EIO=3&transport=polling&t=1447350154198-2&sid=eW7B_kpSat6WfGQSAAAC
Here's the code..
Server - app.js
var server = require('http').createServer();
var io = require('socket.io')(server);
io.on('connection', function(socket) {
socket.emit('connection', 'connected');
socket.on('disconnect', function() {
socket.emit('disconnection', 'not connected');
});
});
server.listen(3000);
Client - index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="//recently.dev:3000/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('<?php echo '//' . $_SERVER['HTTP_HOST'] . ':3000/'; ?>');
socket.on('connection', function(data){
console.log(data);
});
</script>
</head>
<body>
</body>
</html>

I think this is why..
Why is my socket.io using long polling instead of the websocket?
I think older approach was better IMO. There should at least be an option.

Related

socket connection closed before receiving a handshake response

I have written code for socket from :source
My code looks like this.
var port = 8081;
var app = require('http').createServer();
var io = require('socket.io')(app);
app.listen(port);
io.on('connection', function (socket) {
console.log("New user connected.");
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
console.log("server listening on: " + port);
But when I am testing it on here (ws://localhost:8081). I am getting
ERROR: undefined
DISCONNECTED
In console I am getting error:
WebSocket connection to 'ws://localhost:8081/?encoding=text' failed: Connection closed before receiving a handshake response
You need to use the socket.io client library, either getting it, from here, or referencing their CDN from your page like this:
<script src="https://cdn.socket.io/socket.io-1.3.7.js"></script>
Additionally, whenever you start a socket.io server, socket.io will be served from http://<your-server-address>/socket.io/socket.io.js. You should be able to test your server using this simple page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="http://localhost:8081/socket.io/socket.io.js"></script>
<script>
var socket = io('http://localhost:8081/');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
</head>
<body>
</body>
</html>
You can read more about why you can't connect from the websocket.org site on this github issue.

Socket.io connection event not fired

I am beginner to socket.io and trying to make simple hello world like program. It looks like my index.html page loads and connects to the server successfully but the server is not firing the "connection" event. I am sharing my code so that you can figure out what am I doing wrong here.
app.js
var app = require('http');
var io = require('socket.io'), fs = require('fs');
app.createServer(function(req,res){
console.log(req.url);
switch(req.url){
case '/':
fs.readFile('./index.html',function(err,data){
if(err){
res.end('Error');
throw err;
}
res.writeHead(200,{"Context-type":"text/html"});
res.write(data);
res.end();
});
break;
case '/socket.io.js':
fs.readFile('./socket.io.js',function(err,data){
if(err){
res.end('Error');
throw err;
}
res.writeHead(200,{"Context-type":"text/javascript"});
res.write(data);
res.end();
});
}
}).listen(8888);
ios = io.listen(app);
console.log("Listening..");
ios.sockets.on('connection',function(socket){
console.log('connection established.'); //this doesn't show up in CLI
});
Index.html
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Example</title>
<script src="socket.io.js" type="text/javascript"></script>
<script>
var socket = io.connect('http://localhost:8888');
</script>
</head>
<body>
</body>
</html>
Finally I figured it out myself. All I had to do was to
server = require('http').createServer()
and then pass that server to the listener of socket io i.e.
io.listen(server);
What I was doing wrong that I was passing app = require('http'); to the io.listen(app)

NodeJS and socket.io can't send message

today I follow an tutorial by Gonzalo Ayuso at http://gonzalo123.com/2011/05/23/real-time-notifications-part-ii-now-with-node-js-and-socket-io/ but it can't send the message
Here is my server.js
var http = require('http');
var io = require('socket.io');
server = http.createServer(function(req, res){
});
server.listen(8000);
//socket.io
var socket = io.listen(server);
socket.set('transports', ['websocket']);
console.log("Start");
socket.on('connection', function(client){
client.on('message', function(msg){
console.log(msg);
socket.broadcast(msg);
})
});
and the client.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Comet Test</title>
</head>
<body>
<p><a id='customAlert' href="#" onclick='socket.send("customAlert")'>publish customAlert</a></p>
<p><a id='customAlert2' href="#" onclick='socket.send("customAlert2")'>publish customAlert2</a></p>
<script src="http://localhost:8000/socket.io/socket.io.js" type="text/javascript"></script>
<script type="text/javascript">
// Start the socket
var socket = io.connect('http://localhost:8000');
socket.on('message', function(msg){
console.log(msg);
});
</script>
</body>
</html>
I have edited it just a little bit to run on my server. But the client doesn't send message to server. Can anybody help me? Sorry for my bad English.
I have found out that the client can't connect to server but I don't know why?
My computer is running xampp with apache server. Maybe it's problem?
Updated:
I have just set transports to xhr-polling and it connect success. Why doesn't it accept websocket?
you need to read more carefully the tutorials about socket.io
first of all to call some event, you need to emit the event name:
socket.emit('my other event', { my: 'data' });
thats mean, on your onclick in anchor need to call socket.emit ..., or to call some function like:
function call_my_event(name){
socket.emit(name,{my : 'data'});
}
I have found out my problem. It's about firewall or proxy or something else in my computer block websocket protocol. I tested that code on another computer and it works fine. So I have to re-install my system and websocket is available for me :D

node.js and socket.io

I have created a small server for communication using node.js and socket io
Server:
var express = require('/usr/local/lib/node_modules/express')
var io = require('/usr/local/lib/node_modules/socket.io');
var server = express.createServer();
server.listen(8888,'localhost');
io = io.listen(server);
io.of("namespace").on('connection', function(client){
client.emit("message",'connection successful');
client.on('message', function(m){
console.log("received message"+m);
client.broadcast(m);
});
});
Client:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Comet Test</title>
<script src="http://localhost:8888/socket.io/socket.io.js" type="text/javascript"></script>
</head>
<body>
<p><a id='customAlert' href="#" onclick='sendMessage("customAlert")'>publish customAlert</a></p>
<p><a id='customAlert2' href="#" onclick='sendMessage("customAlert2")'>publish customAl
<script type="text/javascript">
// Start the socket
var socket = io.connect('http://localhost:8888/namespace');
socket.on('error', function (reason){
console.error('Unable to connect Socket.IO', reason);
});
socket.on('message', function(msg){
console.log(msg);
});
function sendMessage(m) {
console.log("sending message"+m);
socket.emit("message",m);
}
</script>
</body>
Communication does not happen, am I doing something wrong here?
If your server is listening on port 8888, you need the client to connect to the server on port 8888.
The following line in your client code:
var socket = io.connect('http://localhost/namespace');
Should read:
var socket = io.connect('http://localhost:8888/namespace');

Problem with first program in socket.io

I am having some problems in executing my first socket.io program. Here is the code
var io=require('socket.io');
var http=require('http');
server =http.createServer(function(req,res) {
res.writeHead(200,{'Content-Type':'text/html'});
res.write('welcome');
res.end();
});
server.listen(7777);
var socket = io.listen(server);
socket.on('connection', function (client) {
console.log('client connected');
client.emit('news', { hello: 'world' });
client.on('another', function (data) {
console.log(data);
});
});
And here is my client side code
<html>
<head>
<script type="text/javascript" src="http://localhost:7777/socket.io/lib/socket.io.js"> </script>
<script type="text/javascript">
var socket = io.connect("http://localhost:7777");
socket.on('news',function(data) {
alert(data);
socket.emit('another',{my:'data'});
});
</script>
</head>
</html>
EDIT: Now, i get an error in my server terminal "info - client protocol unsupported"
what am i doing wrong here? Thanks
You are trying to import a javascript file from the socket io port instead of the web server port.
<script type="text/javascript" src="http://localhost:7777/socket.io/lib/socket.io.js"> </script>
You need to give the correct src parameter. For newer version of socket io, this would be something like socket/socket.io.js.

Resources