Socket
Book a DemoInstallSign in
Socket

em-ws-client

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

em-ws-client

0.2.0
bundlerRubygems
Version published
Maintainers
1
Created
Source

em-ws-client

RFC 6455 Compliant WebSocket client for ruby. See report for compliance: View Autobahn Report

Installation

$ gem install em-ws-client

Sending and Receiving Messages

require "em-ws-client"

EM.run do

  # Establish the connection
  ws = EM::WebSocketClient.new("ws://server/path")

  # Simple echo
  # If the binary flag is set, then
  # the message is a string encoded as ASCII_8BIT
  # otherwise it's encoded as UTF-8
  ws.onmessage do |msg, binary|
    conn.send_message msg, binary
  end

  # Send a text message
  ws.send_message "hello!"

  # Send a binary message
  ws.send_message [2,3,4].pack("NnC"), true

end

Control Events

require "em-ws-client"

EM.run do

  # Establish the connection
  ws = EM::WebSocketClient.new("ws://server/path")

  ws.onopen do
    # fire away
  end

  ws.onclose do |code, explain|
    # could be good, or not
  end

  ws.onping do |msg|
    # we automatically pong, but this is here
  end

  ws.onerror do |code, message|
    # errors close the connection (per spec), but you can at
    # least learn why with this
  end

  ws.ping "ping"

  ws.onpong do |msg|
    # mes -> what you called ping with
  end

end

Streaming API (Design Only)

Thoughts appreciated.


# streaming in
ws.onstream do |stream|
  
  # stream started
  # stream.binary?

  stream.ondata do |chunk|
    # ...
  end

  stream.onclose do
    # stream finished
  end
end

# streaming out
ws.stream(true) do |stream|
  100.times do |i|
    stream << [i].pack("N")
  end
  stream.close
end

FAQs

Package last updated on 14 Apr 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.