Node JS Socket.io & HTTPS

116 views
Skip to first unread message

Julien Lereverend

unread,
Feb 24, 2015, 11:32:46 AM2/24/15
to nod...@googlegroups.com
Hello everybody,

When i try to access Node JS with socket.io and https i have an error.

My app
var https = require('https');
var fs = require('fs');
var elasticsearch = require('elasticsearch');

var client = new elasticsearch.Client({
    host
: 'localhost:9200'/*,
    log: 'trace'*/

});

var options = {
  key
: fs.readFileSync('key.pem'),
  cert
: fs.readFileSync('cert.pem')
};

var serverhttps = https.createServer(options, function (req, res) {});

var io = require('socket.io').listen(serverhttp);
io
.set('origins', '*:*');

// Quand on client se connecte
io
.sockets.on('connection', function (socket) {
    console
.log('Connecte');

   
// Quand on reçoit un appel search
    socket
.on('search', function (obj) {
        client
.search(obj.searchQuery, function (error, reponse) {    
            socket
.emit('returnResultElasticsearch', {'searchType':obj.searchType, 'searchResult':reponse});
       
});
   
});
   
   
// Quand on reçoit un appel suggest
    socket
.on('suggest', function (obj) {
        client
.suggest(obj.suggestQuery, function (error, reponse) {    
            socket
.emit('returnResultAutoCompletion', {'suggestType':obj.suggestType, 'suggestResult':reponse});
       
});
   
});
});

serverhttp
.listen(8080, "0.0.0.0");



My client
var socket = io.connect('https://my.domain:8080', {
   
'reconnect':false
});


I have the message error
https://my.domain:8080/socket.io/?EIO=3&transport=polling&t=1424795460423-0
A multi-origin request was blocked: the policy "Same origin" does not allow to see remote resource located in https://my.domain:8080/socket.io/?EIO=3&transport=polling&t=1424795460423-0. this an be fixed by moving the resource on the samin domain or by enabling CORS.

Can you help me please ?

Dave Sag

unread,
Feb 25, 2015, 9:56:09 AM2/25/15
to nod...@googlegroups.com
Here's how I am doing it (business specific logic trimmed out)

var app = require('express')()
var http = require('http').Server(app)
var io = require('socket.io')(http)
var port = process.env.PORT || 8000

http.listen(port, function () {
  console.log("Server listening on port " + port)
})

io.on('connection', function (socket) {
  console.log("Connection opened")

  socket.on('disconnect', function () {
    console.log("Socket disconnected")
  })

  socket.on('my-event', function(data){
    console.log("got data for my-event", data)
  })
})

hope that helps

d

Adam Reynolds

unread,
Feb 25, 2015, 10:25:24 AM2/25/15
to nod...@googlegroups.com
CORS is a security browser issue. The browser is preventing a cross site scripting attack. You need to look at sending the correct headers that tell your browser client that all is ok. 

Please google CORS and read up on the issue.

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/0ab19fd4-4903-481f-9ff2-18fbf639c983%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Ruediger Seiffert

unread,
Feb 26, 2015, 10:02:52 AM2/26/15
to nod...@googlegroups.com
Please compare this line
var serverhttps = https.createServer(options, function (req, res) {});
with this line:
var io = require('socket.io').listen(serverhttp);

It looks like you are attaching socket.io to the wrong server (first one is serverhttps, second is serverhttp instead)

HTH

Ruediger Seiffert

unread,
Feb 26, 2015, 10:02:52 AM2/26/15
to nod...@googlegroups.com
Hi, just a guess: You are declaring your https-server with "serverhttps" (approx. line 14) but afterwards (on line 16) you attach your socket.io to "serverhttp" (without the last "s")


Am Dienstag, 24. Februar 2015 17:32:46 UTC+1 schrieb Julien Lereverend:
Reply all
Reply to author
Forward
0 new messages