0

I am following this tutorial on creating a docker/socket.io/aws app. Following the tutorial completely, the app works. However, if I try to access the socket from outside the self hosted client I can only connect to the server with http, but if I have an Android app or iOS app try to connect directly to the socket it never completes the handshake?

My guess is that Alpine Linux has some sort of reverse proxy issue? Do I need to setup some nginx.conf or something to allow a TCP connection directly to port 8080?

DOCKERFILE:

FROM mhart/alpine-node:0.10.48 AS build
WORKDIR /srv
ADD package.json .
RUN npm install --prod
ADD . .

FROM mhart/alpine-node:base-0.10.48
COPY --from=build /srv .
EXPOSE 8080
CMD ["node", "server.js"] 

SOCKET SERVER:

if (typeof String.prototype.startsWith != 'function') {
  String.prototype.startsWith = function (str){
    return this.slice(0, str.length) == str;
  };
}

function log_me(msg){
  var ts = new Date(new Date().getTime() - (3600000*4));
  var tss = ts.toString();
  tss = tss.substring(0, tss.indexOf(' GMT'));
  console.log(tss + ": " + msg);
}

var io = require('socket.io').listen(8080,'0.0.0.0');
io.set('log level', 2);
io.set('transports', ['flashsocket', 'polling', 'websocket']);
io.set('origins', '*:*');

var socket = io.connect('http://0.0.0.0:8080');

io.sockets.on('connection', function (socket) {
  socket.on('join_channel', function(channel){
    log_me("join_channel: " + channel);
    socket.channel = channel;
    socket.join(channel);
  });
  socket.on('alert_ambulance', function(channel, data){
    log_me("alert_ambulance: " + channel + " - " + data);
    io.sockets.in(channel).emit('alert', data);
  });
  socket.on('call_ambulance', function(channel, data){
    log_me("call_ambulance: " + channel + " - " + data);
    io.sockets.in(channel).emit('call', data);
  });
  socket.on('proceed', function(channel, data){
    log_me("proceed: " + channel + " - " + data);
    io.sockets.in(channel).emit('proceed', data);
  });
  socket.on('instruct', function(channel, data){
    log_me("instruct: " + channel + " - " + data);
    io.sockets.in(channel).emit('instruct', data);
  });
  socket.on('status_update', function(channel, data){
    log_me("status_update: " + channel + " - " + data);
    io.sockets.in(channel).emit('status_update', data);
  });
  socket.on('ambulance_move', function(channel, data){
    io.sockets.in(channel).emit('ambulance_move', data);
  });
  socket.on('notify_cad', function(channel, data){
    log_me("notify_cad: " + channel + " - " + data);
    io.sockets.in(channel).emit('notify_cad', data);
  });
  socket.on('video_call_response', function(data){
    var obj = JSON.parse(data);
    log_me("video_call_response: " + obj.channel + " - " + data);
    io.sockets.in(obj.channel).emit('video_call_response', data);
  });
  socket.on('video_call_hangup', function(data){
    var obj = JSON.parse(data);
    log_me("video_call_hangup: " + obj.channel + " - " + data);
    io.sockets.in(obj.channel).emit('video_call_hangup', data);
  });
  socket.on('request_video_call', function(data){
    var obj = JSON.parse(data);
    log_me("request_video_call: " + obj.channel + " - " + data);
    io.sockets.in(obj.channel).emit('request_video_call', data);
  });
  socket.on('disconnect', function(){
    log_me("disconnect: " + socket.channel);
    if(socket.channel){
      if(socket.channel.startsWith("a_")){
        var c = socket.channel.replace("a_", "");
        var data = {'ambulance_id' : c, 'status' : 'DISCONNECTED'}
        io.sockets.in(c).emit('status_update', JSON.stringify(data));
      }
    }
    socket.leave(socket.channel);
  });
});

The socket.io server works as you can hit the server with http://blah.com:8080 but anything outside of a browser trying to connect to the socket directly fails.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.