Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7403517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:06:19+00:00 2026-05-29T05:06:19+00:00

By following example from em-websocket gem, I’ve just create simple echo-server that running with

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T05:06:21+00:00Added an answer on May 29, 2026 at 5:06 am

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please derive a concrete example from the following: http://www.urdalen.com/blog/?p=210 ..that shows how
I'm trying to compile the following simple DL library example code from Program-Library-HOWTO with
I need a regex that will give me the following results from each example
I'm running the following example code from Apple - NSString *requestURL = [[NSString alloc]
I'm following an example from Pragmatic Project Automation. I'm running on Windows 7 and
I tried the following example code from the tutorial that came along wxPython2.8 Docs
I just recently started looking into WatiN and was following the example from http://www.codeproject.com/KB/aspnet/WatiN.aspx
I followed an example from the following url that shows how to use the
I'm following the racetrack example from Jason Rudolph's book at InfoQ , using grails-1.2.1.
I have the following example of reading from a buffered reader: while ((inputLine =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.