can socketio listen on two servers?

167 views
Skip to first unread message

Jason Chen

unread,
Oct 7, 2011, 7:28:51 AM10/7/11
to Socket.IO
I have an http and an https server:

httpServer = http.createServer(serverHandler)
httpServer.listen(80)
httpsServer = https.createServer(options, serverHandler)
httpsServer.listen(443)

1. Can I get socketio to listen on both of them? I want broadcasts to
work between both http and https clients and this will not work if I
have two separate socketio instances.

2. Is there a better way to get http AND https to work with socketio?
The recommended nginx recipe http://www.letseehere.com/reverse-proxy-web-sockets
did not work for me as the tcp_proxy module does not support ssl yet.

Daniel Shaw

unread,
Oct 7, 2011, 7:52:56 AM10/7/11
to sock...@googlegroups.com
1. Great use case for RedisStore.
2. I'm going to pass on to others who have more socket.io + ssl experience.

Daniel Shaw
@dshaw

Jason Chen

unread,
Oct 7, 2011, 8:06:50 AM10/7/11
to sock...@googlegroups.com
Cool been meaning to switch to redisstore soon anyways. Where's the best place to look for what changes need to be made to move from memorystore to redisstore?

Best,
Jason Chen

Daniel Shaw

unread,
Oct 7, 2011, 8:19:53 AM10/7/11
to sock...@googlegroups.com
We need to get something up on the wiki. The minimal config is:

var sio = require('socket.io')
, RedisStore = sio.RedisStore
, app = ...
, io = sio.listen(app);

io.configure(function () {
io.set('store', new RedisStore);
});

Daniel Shaw
@dshaw

Jason Chen

unread,
Apr 1, 2012, 9:55:12 PM4/1/12
to sock...@googlegroups.com
I finally got around to trying this again and am still having issues. Here's two configs I tried:

sioRedisStore = new RedisStore()

initSocketIO = (io) ->
  io.configure(->
    io.set('store', sioRedisStore)   # config1
    # io.set('store', new RedisStore())   # config2
  )

  io.sockets.on('connection', (client) ->
    console.log "SOCKETIO connection"
    client.on("message", (message, callback) ->
      console.log "SOCKETIO message"
    )
    client.on("disconnect", ->
      console.log "SOCKETIO disconnect"
    )
    client.on("close", ->
      console.log "SOCKETIO close"
    )
  )

httpIO = socketio.listen(httpServer)
initSocketIO(httpIO)
httpsIO = socketio.listen(httpsServer)
initSocketIO(httpsIO)

In the first config where there is a shared redisstore between the two http and https socketio instances, everything works fine except for rooms seem to be separate. That is if a client from http joins the same room a client from https does, they will not broadcast messages to each other. http -> http and https -> https works fine.

In the second config where each socketio instance has its own redisstore, for some reason a disconnect and close immediately fires after connection (in the logs I will see SOCKETIO connection immediately followed by SOCKETIO disconnect and SOCKETIO close) and the messages handler never fires for either socketio instance.

It seems like the first config is closer to being correct but can't seem to figure out how what I am doing wrong. Any suggestions would be greatly appreciated. Thanks!


Best regards,
Jason Chen

Jason Chen

unread,
Apr 5, 2012, 2:33:01 AM4/5/12
to sock...@googlegroups.com
In case people are interested I ended up avoiding the issue by using nodejitsu's http-proxy to handle the ssl and the app just thinks everything comes from http so no need for multiple servers. To my knowledge this is the only thing at this point to fully support both SSL and websockets? Would still be interested if someone else has a better setup/solution.

fs = require('fs')
http = require('http')
https = require('https')
proxy = require('http-proxy')

options =
  https:
    cert: fs.readFileSync('/path/to.crt')
    key:  fs.readFileSync('/path/to.key')

proxy.createServer(8080, 'localhost').listen(80)
console.info "Proxy listening on port 80 forwarding to port 8080"

proxy.createServer(8080, 'localhost', options).listen(443)
console.info "Proxy listening on port 443 forwarding to port 8080"


Best regards,
Jason Chen

Brian Gruber

unread,
Apr 5, 2012, 8:55:12 AM4/5/12
to sock...@googlegroups.com
I've been using stunnel. 

Sent from my iPhone
Reply all
Reply to author
Forward
0 new messages