Send messages on different channels using sinatra-websocket - ruby

Is there a way to send messages on different channels using the sinatra-websocket gem?
Basically I'm trying to replace Pusher with sinatra-websocket. Here's what I'm doing with Pusher:
Pusher["my_channel_A"].trigger('some_event_type', my_message)
Pusher["my_channel_B"].trigger('another_event_type', my_message)
What would be the equivalent of that syntax in this sinatra-websocket snippet?
request.websocket do |ws|
ws.onopen do
ws.send("Hello World!")
settings.sockets << ws
end
ws.onmessage do |msg|
EM.next_tick { settings.sockets.each{|s| s.send(msg) } }
end
ws.onclose do
warn("websocket closed")
settings.sockets.delete(ws)
end
end

Found an answer to this posted here:
get '/socket/live/game/:id' do
if !request.websocket?
puts "Not a websocket request"
else
request.websocket do |ws|
channel = params[:id]
#con = {channel: channel, socket: ws}
ws.onopen do
ws.send("Hello World!")
settings.sockets << #con
end
ws.onmessage do |msg|
return_array = []
settings.sockets.each do |hash|
#puts hash
#puts hash['channel']
if hash[:channel] == channel
#puts hash[:socket]
return_array << hash
puts "Same channel"
puts return_array
else
puts hash[:channel]
puts channel
puts "Not in same channel"
end
end
EM.next_tick { return_array.each{|s| s[:socket].send(msg) } }
end
ws.onclose do
warn("websocket closed")
settings.sockets.each do |hash|
if hash[:socket] == ws
settings.sockets.delete(hash)
puts "deleted"
else
puts "not deleted"
end
end
end
end
end
end
It's still quite verbose. I guess Pusher abstracts away all this through their API.

Related

How to test WebSockets For Hanami?

Using the following:
Hanami cookbook websockets
IoT Saga - Part 3 - Websockets! Connecting LiteCable to Hanami
I've been able to add WebSockets to Hanami, however as this is for production code I want to add specs; but I can't find information on how to test WebSockets and Hanami using Rspec.
I've been able to find this for RoR but nothing non-Rails specific or Hanami Specific, I have asked on the Hanami Gitter but not gotten a response yet.
Is the TCR gem the only way? I would prefer something simpler but If I must how would I set it up for anycable-go via litecable.
How can I test WebSockets for Hanami using Rspec?
To get this working requires several moving parts, the first is the Socket simulator which simulates the receiving socket on the webserver:
Note: url_path should be customized to what works for your web socket specific endpoint
# frozen_string_literal: true
require 'puma'
require 'lite_cable/server'
require_relative 'sync_client'
class SocketSimulator
def initialize(x_site_id_header: nil)
#server_logs = []
#x_site_id_header = x_site_id_header
end
attr_accessor :server_logs
def client
return #client if #client
url_path = "/ws?connection_token=#{connection_token}"
#client = SyncClient.new("ws://127.0.0.1:3099#{url_path}", headers: headers, cookies: '')
end
def connection_token
#connection_token ||= SecureRandom.hex
end
def user
return #user if #user
email = "#{SecureRandom.hex}#mailinator.com"
password = SecureRandom.hex
#user = Fabricate.create :user, email: email, site_id: site_id, password: password
end
def start
#server = Puma::Server.new(
LiteCable::Server::Middleware.new(nil, connection_class: Api::Sockets::Connection),
Puma::Events.strings
).tap do |server|
server.add_tcp_listener '127.0.0.1', 3099
server.min_threads = 1
server.max_threads = 4
end
#server_thread = Thread.new { #server.run.join }
end
def teardown
#server&.stop(true)
#server_thread&.join
#server_logs.clear
end
def headers
{
'AUTHORIZATION' => "Bearer #{jwt}",
'X_HANAMI_DIRECT_BOOKINGS_SITE_ID' => #x_site_id_header || site_id
}
end
def site_id
#site_id ||= SecureRandom.hex
end
def jwt
#jwt ||= Interactors::Users::GenerateJwt.new(user, site_id).call.jwt
end
end
The next thing is the SyncClient which is a fake client you can use to actually connect to the simulated socket:
# frozen_string_literal: true
# Synchronous websocket client
# Copied and modified from https://github.com/palkan/litecable/blob/master/spec/support/sync_client.rb
class SyncClient
require 'websocket-client-simple'
require 'concurrent'
require 'socket'
WAIT_WHEN_EXPECTING_EVENT = 5
WAIT_WHEN_NOT_EXPECTING_EVENT = 0.5
attr_reader :pings
def initialize(url, headers: {}, cookies: '')
#messages = Queue.new
#closed = Concurrent::Event.new
#has_messages = Concurrent::Semaphore.new(0)
#pings = Concurrent::AtomicFixnum.new(0)
#open = Concurrent::Promise.new
#ws = set_up_web_socket(url, headers.merge('COOKIE' => cookies))
#open.wait!(WAIT_WHEN_EXPECTING_EVENT)
end
def ip
Socket.ip_address_list.detect(&:ipv4_private?).try(:ip_address)
end
def set_up_web_socket(url, headers)
WebSocket::Client::Simple.connect(
url,
headers: headers
) do |ws|
ws.on(:error, &method(:on_error))
ws.on(:open, &method(:on_open))
ws.on(:message, &method(:on_message))
ws.on(:close, &method(:on_close))
end
end
def on_error(event)
event = RuntimeError.new(event.message) unless event.is_a?(Exception)
if #open.pending?
#open.fail(event)
else
#messages << event
#has_messages.release
end
end
def on_open(_event = nil)
#open.set(true)
end
def on_message(event)
if event.type == :close
#closed.set
else
message = JSON.parse(event.data)
if message['type'] == 'ping'
#pings.increment
else
#messages << message
#has_messages.release
end
end
end
def on_close(_event = nil)
#closed.set
end
def read_message
#has_messages.try_acquire(1, WAIT_WHEN_EXPECTING_EVENT)
msg = #messages.pop(true)
raise msg if msg.is_a?(Exception)
msg
end
def read_messages(expected_size = 0)
list = []
loop do
list_is_smaller = list.size < expected_size ? WAIT_WHEN_EXPECTING_EVENT : WAIT_WHEN_NOT_EXPECTING_EVENT
break unless #has_messages.try_acquire(1, list_is_smaller)
msg = #messages.pop(true)
raise msg if msg.is_a?(Exception)
list << msg
end
list
end
def send_message(message)
#ws.send(JSON.generate(message))
end
def close
sleep WAIT_WHEN_NOT_EXPECTING_EVENT
raise "#{#messages.size} messages unprocessed" unless #messages.empty?
#ws.close
wait_for_close
end
def wait_for_close
#closed.wait(WAIT_WHEN_EXPECTING_EVENT)
end
def closed?
#closed.set?
end
end
The last part is a fake channel to test against:
# frozen_string_literal: true
class FakeChannel < Api::Sockets::ApplicationChannel
identifier :fake
def subscribed
logger.info "Can Reject? #{can_reject?}"
reject if can_reject?
logger.debug "Streaming from #{stream_location}"
stream_from stream_location
end
def unsubscribed
transmit message: 'Goodbye channel!'
end
def can_reject?
logger.info "PARAMS: #{params}"
params.fetch('value_to_check', 0) > 5
end
def foo
transmit('bar')
end
end
To use in specs:
# frozen_string_literal: true
require_relative '../../../websockets-test-utils/fake_channel'
require_relative '../../../websockets-test-utils/socket_simulator'
RSpec.describe Interactors::Channels::Broadcast, db_truncation: true do
subject(:interactor) { described_class.new(token: connection_token, loc: 'fake', message: message) }
let(:identifier) { { channel: 'fake' }.to_json }
let(:socket_simulator) { SocketSimulator.new }
let(:client) { socket_simulator.client }
let(:user) { socket_simulator.user }
let(:connection_token) { socket_simulator.connection_token }
let(:channel) { 'fake' }
let(:message) { 'woooooo' }
before do
socket_simulator.start
end
after do
socket_simulator.teardown
end
describe 'call' do
before do
client.send_message command: 'subscribe',
identifier: identifier
end
it 'broadcasts a message to the correct channel' do
expect(client.read_message).to eq('type' => 'welcome')
expect(client.read_message).to eq(
'identifier' => identifier,
'type' => 'confirm_subscription'
)
interactor.call
expect(client.read_message).to eq(
'identifier' => identifier,
'message' => message
)
end
context 'with other connection' do
let(:user2) { Fabricate.create :user }
let(:jwt) { Interactors::Users::GenerateJwt.new(user2, site_id).call.jwt }
let(:site_id) { socket_simulator.site_id }
let(:url_path) { "/ws?connection_token=#{SecureRandom.hex}" }
let(:client2) { SyncClient.new("ws://127.0.0.1:3099#{url_path}", headers: {}, cookies: '') }
before do
client2.send_message command: 'subscribe',
identifier: identifier
end
it "doesn't broadcast to connections that shouldn't get it" do
aggregate_failures 'broadcast!' do
expect(client2.read_message).to eq('type' => 'welcome')
expect(client2.read_message).to eq(
'identifier' => identifier,
'type' => 'confirm_subscription'
)
expect(client.read_message).to eq('type' => 'welcome')
expect(client.read_message).to eq(
'identifier' => identifier,
'type' => 'confirm_subscription'
)
interactor.call
sleep 1
expect(client.read_message).to eq(
'identifier' => identifier,
'message' => message
)
expect { client2.close }.not_to raise_exception
end
end
end
end
end

TwiML Exporting to Sinatra, but not actually sending SMS Messages

I am having an issue with outbound SMS thru TwiML Message functions. I am able to see my post on my Sinatra server session here:
== Sinatra (v2.0.0) has taken the stage on 4567 for development with backup from WEBrick
[2018-01-24 15:30:55] INFO WEBrick::HTTPServer#start: pid=67403 port=4567
<?xml version="1.0" encoding="UTF-8"?><Response><Message to="+1904XXXXXXX"><Body>Text Message Test for Devotional App. Please reply.</Body></Message><Message to="+1786XXXXXXX"><Body>Text Message Test for Devotional App. Please reply.</Body></Message><Message to="+1904XXXXXXX"><Body>Text Message Test for Devotional App. Please reply.</Body></Message></Response>
50.235.219.155 - - [24/Jan/2018:15:31:16 -0500] "POST /message HTTP/1.1" 200 - 0.0022
::1 - - [24/Jan/2018:15:31:16 EST] "POST /message HTTP/1.1" 200 0
- -> /message
I see the inbound logs here, but nothing outbound. I have even elevated this to a paid account to make sure it wasn't a trail thing.
This code is based on this walkthru.
My full ruby code for the app is here:
require 'yaml'
require 'open-uri'
require 'sinatra'
require 'twilio-ruby'
MY_NUMBER = '+1904XXXXXXXX'
def spreadsheet_url
'contacts.yml'
end
def sanitize(number)
"+1" + number.gsub(/^1|\D/, "")
end
def data_from_spreadsheet
file = open(spreadsheet_url).read
YAML.load(file)
end
def contacts_from_spreadsheet
contacts = {}
data_from_spreadsheet.each do |entry|
name = entry['name']
number = entry['phone_number'].to_s
contacts[sanitize(number)] = name
end
contacts
end
def contacts_numbers
contacts_from_spreadsheet.keys
end
def contact_name(number)
contacts_from_spreadsheet[number]
end
get '/' do
"Devotional Broadcast is Up & Running!"
end
get '/message' do
"Things are Working!"
end
post '/message' do
from = params['From']
body = params['Body']
media_url = params['MediaUrl0']
if from == MY_NUMBER
twiml = send_to_contacts(body, media_url)
else
twiml = send_to_me(from, body, media_url)
end
content_type 'text/xml'
puts twiml
end
def send_to_contacts(body, media_url = nil)
response = Twilio::TwiML::MessagingResponse.new do |r|
contacts_numbers.each do |num|
r.message to: num do |msg|
msg.body body
msg.media media_url unless media_url.nil?
end
end
end
puts response
end
def send_to_me(from, body, media_url = nil)
name = contact_name(from)
body = "#{name} (#{from}):\n#{body}"
response = Twilio::TwiML::MessagingResponse.new do |r|
r.message to: MY_NUMBER do |msg|
msg.body body
msg.media media_url unless media_url.nil?
end
end
puts response
end
Any help or insight would be great! Thanks!
I think I got it. Swapped out puts for .to_s Documentation Example here: Receive & Reply to SMS & MMS
post '/message' do
from = params['From']
body = params['Body']
media_url = params['MediaUrl0']
if from == MY_NUMBER
twiml = send_to_contacts(body, media_url)
else
twiml = send_to_me(from, body, media_url)
end
content_type 'text/xml'
twiml.to_s
end
def send_to_contacts(body, media_url = nil)
response = Twilio::TwiML::MessagingResponse.new do |r|
contacts_numbers.each do |num|
r.message to: num do |msg|
msg.body body
msg.media media_url unless media_url.nil?
end
end
end
response.to_s
end
def send_to_me(from, body, media_url = nil)
name = contact_name(from)
body = "#{name} (#{from}):\n#{body}"
response = Twilio::TwiML::MessagingResponse.new do |r|
r.message to: MY_NUMBER do |msg|
msg.body body
msg.media media_url unless media_url.nil?
end
end
response.to_s
end

NameError - uninitialized constant Twilio::TwiML::Response (Possibly from old API Code?)

I am in need of some help with setting up a Twilio SMS Broadcast App running on Sinatra. They build is based off this tutorial: Send Mass SMS Broadcasts in Ruby
When I make an HTTP POST I get this message in my Terminal when running Sinatra & Ngrok.
NameError - uninitialized constant Twilio::TwiML::Response
Did you mean? Twilio::Response:
broadcast.rb:75:in `send_to_me'
broadcast.rb:53:in `block in <main>'
The code it is having an issue with is:
def send_to_contacts(body, media_url = nil)
response = Twilio::TwiML::Response.new do |r|
contacts_numbers.each do |num|
r.Message to: num do |msg|
msg.Body body
msg.Media media_url unless media_url.nil?
end
end
end
response.text
end
def send_to_me(from, body, media_url = nil)
name = contact_name(from)
body = "#{name} (#{from}):\n#{body}"
response = Twilio::TwiML::Response.new do |r|
r.Message to: MY_NUMBER do |msg|
msg.Body body
msg.Media media_url unless media_url.nil?
end
end
response.text
end
I have noticed most new Twilio walkthrus are now using API Auths & Tokens with an
#client = Twilio::REST::Client.new account_sid, auth_token
Is this something I need to be implementing? Any guidance on how I can migrate these two methods to that type of format and keep my features?
Thanks!
Update:
Twilio::TwiML::Response has been replaced by Twilio::TwiML::VoiceResponse & Twilio::TwiML::MessagingResponse. It worked when I changed the code to this:
def send_to_contacts(body, media_url = nil)
response = Twilio::TwiML::MessagingResponse.new do |r|
contacts_numbers.each do |num|
r.message to: num do |msg|
msg.body body
msg.media media_url unless media_url.nil?
end
end
end
puts response
end
def send_to_me(from, body, media_url = nil)
name = contact_name(from)
body = "#{name} (#{from}):\n#{body}"
response = Twilio::TwiML::MessagingResponse.new do |r|
r.message to: MY_NUMBER do |msg|
msg.body body
msg.media media_url unless media_url.nil?
end
end
puts response
end

ruby net::ssh does not print stdout data on channel.on_data

I wanted to run a remote command with ruby's net::ssh and was hoping to print the output stdout but i don't see anything printed at the below code at channel.on_data
see test code:
Net::SSH.start('testhost', "root", :timeout => 10) do |ssh|
ssh.open_channel do |channel|
channel.exec('ls') do |_, success|
unless success
puts "NOT SUCCEESS:! "
end
channel.on_data do |_, data|
puts "DATAAAAAA:! " # ======> Why am i not getting to here?? <=======
end
channel.on_extended_data do |_, _, data|
puts "EXTENDED!!!"
end
channel.on_request("exit-status") do |_, data|
puts "EXIT!!!! "
end
channel.on_close do |ch|
puts "channel is closing!"
end
end
end
end
and the output is:
channel is closing!
why don't i get into the block on_data? I want to grab the stdout.
note that i know the client code is able to ssh to the remote server because when I asked the command to be ls > ls.log I saw that ls.log on target host.
Note that opening a channel is asynchronous, so you have to wait for the channel to do anything meaningful, otherwise you are closing the connection too soon.
Try this:
Net::SSH.start('test', "root", :timeout => 10) do |ssh|
ch = ssh.open_channel do |channel|
channel.exec('ls') do |_, success|
unless success
puts "Error"
end
channel.on_data do |_, data|
puts data
end
channel.on_extended_data do |_, _, data|
puts data
end
channel.on_request("exit-status") do |_, data|
puts "Exit"
end
channel.on_close do |ch|
puts "Closing!"
end
end
end
ch.wait
end

EM::WebSocket.run as well as INotify file-watching

This is the code I currently have:
#!/usr/bin/ruby
require 'em-websocket'
$cs = []
EM.run do
EM::WebSocket.run(:host => "::", :port => 8085) do |ws|
ws.onopen do |handshake|
$cs << ws
end
ws.onclose do
$cs.delete ws
end
end
end
I would like to watch a file with rb-inotify and send a message to all connected clients ($cs.each {|c| c.send "File changed"}) when a file changes. The problem is, I do not understand EventMachine, and I can't seem to find a good tutorial.
So if anyone could explain to me where to put the rb-inotify-related code, I would really appreciate it.
Of course! As soon as I post the question, I figure it out!
#!/usr/bin/ruby
require 'em-websocket'
$cs = []
module Handler
def file_modified
$cs.each {|c| c.send "File was modified!" }
end
end
EM.run do
EM.watch_file("/tmp/foo", Handler)
EM::WebSocket.run(:host => "::", :port => 8085) do |ws|
ws.onopen do |handshake|
$cs << ws
end
ws.onclose do
$cs.delete ws
end
end
end

Resources