I'm trying to communicate with CoinEx (cryptocurrency exchange) API via WebSocket using Ruby 2.6. I have the following code:
require 'faye/websocket'
require 'eventmachine'
url = 'wss://socket.coinex.com/'
# url = 'wss://stream.binance.com:9443/ws/ltcbtc#miniTicker'
EM.run do
ws = Faye::WebSocket::Client.new(url)
ws.on :open do |event|
p [:open]
...
end
ws.on :message do |event|
p [:message]
...
end
ws.on :close do |event|
p [:close, event.code, event.reason]
...
end
end
When I run this code, I always see [:close, 1006, ""] immediately, without [:open]. I hacked deeply into gems (faye/websocket and eventmachine) and added some debug output to have better understanding of what is going on. Now my traceback is as follows:
/home/chernish2/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/faye-websocket-0.10.9/lib/faye/websocket/client.rb:87:in `unbind'
/home/chernish2/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/eventmachine-1.2.7/lib/eventmachine.rb:1483:in `event_callback'
/home/chernish2/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/eventmachine-1.2.7/lib/eventmachine.rb:195:in `run_machine'
/home/chernish2/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/eventmachine-1.2.7/lib/eventmachine.rb:195:in `run'
/home/chernish2/soft/trader2/test/ws_test.rb:36:in `<main>'
emit_error(), message=Errno::ENETUNREACH
[:close, 1006, ""]
Which really doesn't make any sense to me since another URL
url = 'wss://stream.binance.com:9443/ws/ltcbtc#miniTicker'
works just fine, and when I'm using https://github.com/altangent/ccxws library (nodejs) it connects to CoinEx using exactly the same URL as in my code without any troubles which means I don't have problems connecting to CoinEx WS endpoint.
So what is wrong with my code? Thank you in advance!
Oh, it is working now, without any changes from my side. Seems like there was some problems on the server side.
I'm trying to get a pure command line oauth flow for an installed app and it's not easy to piece this together... Docs are sorely lacking... I started with the drive example (https://github.com/google/google-api-ruby-client-samples/tree/master/drive) but when it gets to client.authorization = flow.authorize(file_storage) it tries to start webrick to put up a web page. I need something that works similarly to the CLI tools provided by google: it needs to print out the URL I need to visit and then read in the response that I can copy&paste. Is this possible with the google ruby client?
Looks like the following monkey-patch works:
module Google
class APIClient
class InstalledAppFlow
def authorize_cli(storage)
puts "Please visit: #{#authorization.authorization_uri.to_s}"
printf "Enter the code: code="
code = gets
#authorization.code = code
#authorization.fetch_access_token!
if #authorization.access_token
if storage.respond_to?(:write_credentials)
storage.write_credentials(#authorization)
end
#authorization
else
nil
end
end
end
end
end
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this question
I'm currently using em-websocket with Event Machine. It works great, but I also want to provide long polling and/or Flash fall-backs for browsers that don't support Web Sockets (and also so I can run it on Heroku).
I'm basically looking for a Ruby version of Socket.IO, or enough libraries to piece together to get the features I want. I've seen some examples that use Socket.IO, Redis, and a Ruby library that interacts with the Redis DB, but I'd rather keep it simple and just keep it all in Event Machine, rather than having to manage 3 applications instead of one.
Check out Faye - https://github.com/faye/faye.
You can do this with Socket.IO on the client side and em-websocket with async_sinatra and Thin on the server-side. See here for some info on the topic.
I was searching for the same and ended up writing the Plezi websocket framework which I wanted to make easier and more intuitive to use... You can even use it inside your Rails/Sintra app (it will replace your Rack server with Iodine if you do so, and both apps will share the same network connection and process)...
a simple websocket chat/echo server - running over the websocket echo sample page - can look something like this:
require 'plezi'
class BroadcastCtrl
def index
redirect_to 'http://www.websocket.org/echo.html'
end
def on_message data
# the following two lines are the same as:
# self.class.broadcast :_send_message, data
broadcast :_send_message, data
_send_message data
end
def _send_message data
response << data
end
end
route '/', BroadcastCtrl
This is very comfortable for a long-pulling fallback position, as the framework supports both RESTful HTTP and HTTP Streaming.
You can also look into the Plezi client or using any Plezi's Auto-Dispatch feature for auto-dispatching any JSON event to a method. This makes it super easy to write an API for both Websockets and AJAX (AJAJ actually).
Here's a more complicated example, showcasing auto-dispatching, some recursive method calling (using broadcasting, writing data to all the connected clients), AJAX v.s Websoocket recognition, http only requests and websocket only events.
require 'plezi'
class BroadcastCtrl
#auto_dispatch = true
def index event = nil
{event: 'update', target: 'body',
data: 'my content'}.to_json
end
def chat event = nil, broadcast = false
if broadcast # it's recursive broadcasting
return write(event.to_json)
end
if event == nil #it's AJAX
msg = params[:msg]
else # it's websockets
msg = event[:msg]
end
self.class.broadcast :chat, ({event: 'chat', msg: msg}), true
end
def http_only
{event: 'foo', data: 'bar'}.to_json
end
protected
def websocket_only event
{event: 'foo', data: 'bar'}.to_json
end
end
route '/', BroadcastCtrl
The framework also supports easy and native Redis integration, so that broadcasts could propagate through different processes or machines seamlessly.
It also supports slim, haml, sass, coffee-script and hrb templates, so it's possible to move the whole application to one framework, instead of running Sinatra/Rails with a parallel real-time solution (via middleware, via a different app or via a different port access).
...but, to each their own, I guess.
I would like to write an application in Ruby using Qt which will communicate over the network with other instances.
How can I integrate Qt's event loop with DRb or EventMachine?
EDIT:
I found the answer when I will have more time I will post it
require 'eventmachine'
require 'Qt4'
app = Qt::Application.new(ARGV)
hello_button = Qt::PushButton.new("Hello EventMachine")
hello_button.resize(100,20)
hello_button.show
EventMachine.run do
EM.add_periodic_timer(0.01) do
app.process_events
end
end
Im using Pony to send email withing my sinatra applications. But the problem - i cant figure out how to debug or test it. Lest say, in php you can configure sendmail fake app (in php.ini) that will store all outgoing email as plain textfiles with all data in it.
How about ruby apps? Is it possible?
You surely found the solution already yourself
In the pony.rb file there is this code part which sends the mail:
def self.transport(tmail)
..
end
You can simply add a method to return the env:
def debug?
true #false
end
and do something special if debug mode is on
def self.transport(tmail)
puts "Debug message" if debug?
if File.executable? sendmail_binary
transport_via_sendmail(tmail)
else
transport_via_smtp(tmail)
end
end