rubywebsocketeventmachineem-websocket

How to send data in some format (JSON/XML/etc) from server to client by using web-socket


By following example from em-websocket gem, I've just create simple echo-server that running with Sinatra framework. The problem is I don't understand how to use some format to send message instead of plain text. For now, the code looks like:

EventMachine::WebSocket.start(host: '0.0.0.0', port: 8080, debug: true) do |ws|
  ws.onmessage { |msg|
    ws.send msg 
  }
  ...
}

I would like to send message in some format, like a Hash:

ws.onmessage { |msg|
  hash_message = {}
  hash_message[:time] = Time.now.strftime("%T")
  hash_message[:text] = msg
  ws.send hash_message
}

And on client side, use it for building nice message box with time and text divs (time is just example in this case). e.g, use

  ws.onmessage = (evt) ->
    write_message(evt.data.time, 
                  evt.data.text)

instead of

  ws.onmessage = (evt) ->
    write_message evt.data

Solution

  • It was fairly simple. Generate json string on client side:

    send_message = (text) ->
      ws.send JSON.stringify({event:"user_message", data: {text: text}})
    

    parse this string on the server, and send it back to client (with added time):

    EventMachine::WebSocket.start(host: '0.0.0.0', port: 8080, debug: true) do |ws|
      ws.onmessage { |msg|
        msg = JSON.parse(msg)
    
        ws.send JSON.generate({
          time: Time.now.strftime("%T"),
          text: msg["data"]["text"]
        })
      }
      ...
    end
    

    and display this message on client when it get it

    window.onload = -> 
      ws.onmessage = (evt) ->
        data = JSON.parse(evt.data)
        write_message(data.time, 
                      data.text)