I created a live chat on a rails application and I am using EventMachine to run a Websocket to have live chat. The live chat works on localhost on several machines.
It also worked on an Amazon EC2 instance in development on ubuntu, but when I tried to move it to production the live chat is no longer working. No data is being sent back and forth.
Here is my controller:
@clients = []
EM.run do
EM::WebSocket.start(host: "0.0.0.0", port: 3060) do |ws|
crypt = ActiveSupport::MessageEncryptor.new(ENV['SECRET_KEY_BASE'])
ws.onopen do |handshake|
conversation_data = crypt.decrypt_and_verify(handshake.query_string)
@clients << {socket: ws, conv_info: conversation_data}
end
ws.onclose do
ws.send "Closed."
@clients.delete ws
end
ws.onmessage do |data|
data = data.split('L56HTY9999')
body = data[0]
key = data[-1]
conversation = crypt.decrypt_and_verify(key)
new_message = Message.new(body: body, user_id: conversation[:user_id], conversation_id: conversation[:conversation_id])
if new_message.save
@clients.each do |socket|
if socket[:conv_info][:conversation_id] == conversation[:conversation_id]
socket[:socket].send new_message.chat_show
end
end
else
if socket[:conv_info][:user_id] == conversation[:user_id]
socket[:socket].send new_message.errors.to_json
end
end
end
end
end
This is my javascript on the page:
function addMessage(msg) {
var content = JSON.parse(msg);
var context = {
userImage: content.image,
author: content.username,
convoText: content.body,
time: content.created_at
};
var source = $('#add-newest-comment').html();
var template = Handlebars.compile(source);
var newComment = template(context);
$('#container').append(newComment);
}
var key = '<%= @conv_id %>';
var socket, host;
host = "ws://0.0.0.0:3060?"+ key;
function onErrors(msg){
$("#chat-log .error").text( msg);
setTimeout(function() {
$( "#chat-log .error" ).text("");
}, 3000);
}
function socketHealth(msg){
console.log(msg);
}
function connect() {
try {
socket = new WebSocket(host);
socketHealth("Socket State: " + socket.readyState);
socket.onopen = function() {
socketHealth("Socket Status: " + socket.readyState + " (open)");
}
socket.onclose = function() {
socketHealth("Socket Status: " + socket.readyState + " (closed)");
}
socket.onmessage = function(msg) {
addMessage(msg.data);
}
} catch(exception) {
onErrors("Error: " + exception);
}
}
$(function() {
connect();
});
function send() {
var text = $("#message").val();
if (text == '') {
onErrors("Please Enter a Message");
return;
}
try {
socket.send(text + 'L56HTY9999' + key);
} catch(exception) {
onErrors("Failed To Send")
}
$("#message").val('');
}
$('#message').keypress(function(event) {
if (event.keyCode == '13') { send(); }
});
$("#disconnect").click(function() {
socket.close()
});
I'm running my server with this command:
bundle exec thin start -p 80 -d -l thin.log -P thin.pid
My site is live at http://puppyplaydate.co/
The console logs this
Socket State: 0
messages:153 WebSocket connection to 'ws://0.0.0.0:3060/?aVI0OFVHS2VET3d4OTQ4M2s4Rk5VMld1eVlXOUFoVUFxT1JMSm5LWDJOUEhScU50MzFub2JMQ3Y4NG1uekl3TS0tYnlBU1ZRclErK3QxWERHN0taSTVzUT09--01ec4a49ce0aeef59bee5d5b8e13d7916abc20e1' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED
messages:148 Socket Status: 3 (closed)
I used netstat to check that the server was listening at that port and got this back:
tcp 0 0 *:3060 *:* LISTEN
I researched online and someone said they used "daemon" gem to run the websocket on production? I'm not sure if that's something that I need.
I'm really stuck, thank you in advance for your help!