Skip to content

Commit

Permalink
Fixed conflicts and incorporated Minja & Ada's Websocket config
Browse files Browse the repository at this point in the history
  • Loading branch information
bdieu178 committed Aug 7, 2018
2 parents e7c3ec6 + 4eed4ea commit ff77fb0
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 130,561 deletions.
52 changes: 52 additions & 0 deletions app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
require 'plezi'
require 'iodine'
require_relative 'app/controllers/eventstream'

## Set environment, working directory, load gems and create logs
#ENV['ENV'] ||= ENV['RACK_ENV'] ||= ENV['RAILS_ENV'] # production ENV will render SASS as compressed.
## Using pathname extentions for setting public folder
require 'pathname'
## Set up root object, it might be used by the environment and\or the plezi extension gems.
Root ||= Pathname.new(File.dirname(__FILE__)).expand_path
## If this app is independant, use bundler to load gems (including the plezi gem).
## otherwise, use the original app's Gemfile and Plezi will automatically switch to Rack mode.
require 'bundler'
Bundler.require(:default, ENV['ENV'].to_s.to_sym)
# Load all the code from a subfolder called 'app'
Dir[File.join '{controllers}', '**', '*.rb'].each { |file| load File.expand_path(file) }
# Load all the code from a subfolder called 'lib'
Dir[File.join '{lib}', '**', '*.rb'].each { |file| load File.expand_path(file) }


#Setting the port
#Iodine::DEFAULT_HTTP_ARGS[:port] = 4860

## Logging
#::Iodine::DEFAULT_HTTP_ARGS[:log] = 1 if ::Iodine::DEFAULT_HTTP_ARGS[:log].nil?
#Iodine::DEFAULT_HTTP_ARGS[:public] ||= './public'


# # Optional Scaling (across processes or machines):
ENV['PL_REDIS_URL'] ||= ENV['REDIS_URL'] ||
ENV['REDISCLOUD_URL'] ||
ENV['REDISTOGO_URL'] ||
nil # "redis://:[email protected]:6389/0"
# # redis channel name should be changed IF using the same Plezi code within
# # more then one application (i.e., using both Rails and Plezi together).
# Plezi.app_name = 'yacs_websocket_a027e2d668a7dc21c59fe56f018432ea'

# Map the views folder to the template root (for the {#render} function).
#Plezi.templates = Root.join('views').to_s

# load routes.
load Root.join('routes.rb').to_s

App = Proc.new do |env|
if(env['rack.upgrade?'.freeze] == :websocket)
env['rack.upgrade'.freeze] = EventStream.new
[0, {}, []]
else
[200, {"Content-Length" => "12", "Content-Type" => "text/plain"}, ["Hello World!"]]
end

end
2 changes: 1 addition & 1 deletion app/consumers/courses_consumer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
class CourseConsumer < ApplicationConsumer
def consume
EventStream.new.notify(params)
puts "Consumer message sent to websocket"
puts "CoursesConsumer sent message to websocket"
end
end
4 changes: 2 additions & 2 deletions app/consumers/sections_consumer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

class SectionConsumer < ApplicationConsumer
def consume
EventStream.new.notify(params)
puts "Consumer message sent to websocket"
EventStream.new.notify(params)
puts "SectionsConsumer sent message to websocket"
end
end
29 changes: 17 additions & 12 deletions app/controllers/eventstream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,29 @@
require 'iodine'

class EventStream
#@auto_dispatch = true
@auto_dispatch = true
def index
render 'client'
end

def on_open client
puts "WS connection open"
::Iodine::subscribe channel: "notifications"
end
#def on_open
# puts "WS connection open"
# ::Iodine::subscribe channel: "notifications"
#end

def notify(data)
@notification = data.to_s
::Iodine::publish channel: "notifications", message: "#{@notification}"
end

def on_message client, data
::Iodine::subscribe :notifications
::Iodine::publish :notifications , "#{@data}"
end
#def on_message
# ::Iodine::subscribe :notifications
# ::Iodine::publish :notifications , "#{@data}"
#end

def on_close
end
#def on_close
# publish "notifications"
#end
end
Plezi.route '/', EventStream
#test location: ws://localhost:3000/notifications/

52 changes: 3 additions & 49 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,53 +1,7 @@
# Default Rack interface
require 'plezi'
require 'iodine'

# encoding: UTF-8
# load the application
## Set environment, working directory, load gems and create logs
#ENV['ENV'] ||= ENV['RACK_ENV'] ||= ENV['RAILS_ENV'] # production ENV will render SASS as compressed.
## Using pathname extentions for setting public folder
require 'pathname'
## Set up root object, it might be used by the environment and\or the plezi extension gems.
Root ||= Pathname.new(File.dirname(__FILE__)).expand_path
## If this app is independant, use bundler to load gems (including the plezi gem).
## otherwise, use the original app's Gemfile and Plezi will automatically switch to Rack mode.
require 'bundler'
Bundler.require(:default, ENV['ENV'].to_s.to_sym)

# Load all the code from a subfolder called 'app'
Dir[File.join '{controllers}', '**', '*.rb'].each { |file| load File.expand_path(file) }
# Load all the code from a subfolder called 'lib'
Dir[File.join '{lib}', '**', '*.rb'].each { |file| load File.expand_path(file) }

#Setting the port
#Iodine::DEFAULT_HTTP_ARGS[:port] = 4860

## Logging
#Iodine::DEFAULT_HTTP_ARGS[:log] = 1 if Iodine::DEFAULT_HTTP_ARGS[:log].nil?

# # Optional Scaling (across processes or machines):
ENV['PL_REDIS_URL'] ||= ENV['REDIS_URL'] ||
ENV['REDISCLOUD_URL'] ||
ENV['REDISTOGO_URL'] ||
nil # "redis://:[email protected]:6389/0"
# # redis channel name should be changed IF using the same Plezi code within
# # more then one application (i.e., using both Rails and Plezi together).
# Plezi.app_name = 'yacs_websocket_a027e2d668a7dc21c59fe56f018432ea'

# Map the views folder to the template root (for the {#render} function).
#Plezi.templates = Root.join('views').to_s

#module App
# def self.call(env)
# if(env['rack.upgrade?'.freeze] == :websocket)
# env['rack.upgrade'.freeze] = EventStream.new
# return [0, {}, []]
# end
# return [200, {"Content-Length" => "12", "Content-Type" => "text/plain"}, ["Hello World!"]]
# end
#end

#run App
#Iodine::DEFAULT_HTTP_ARGS[:public] ||= './public'
load ::File.expand_path(File.join('..', 'app.rb'), __FILE__)

run App
run Plezi.app
2 changes: 1 addition & 1 deletion entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
nohup bundle exec iodine -p 4860 > iodine.out 2>&1 &
nohup bundler exec iodine -p 4860 -www ./public -v > iodine.out 2>&1 &
bundle exec karafka server
20 changes: 20 additions & 0 deletions iodine.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
* Listening on port 4860

Server is running 2 workers X 2 threads, press ^C to stop
Running Plezi version: 0.15.1

Starting up Iodine HTTP Server on port 4860:
* Ruby v.2.5.1
* Iodine v.0.4.19
* 1048576 max concurrent connections / open files
* Serving static files from ./public

* 6 is running.
* 14 is running.
172.18.0.1 - - [Tue, 6 Aug 2018 03:14:14 GMT] "GET /notifications HTTP/1.1" 101 -- 300ms
Iodine caught an unprotected exception - LocalJumpError: no block given
/usr/src/app/app/controllers/eventstream.rb:12:in `subscribe'
/usr/src/app/app/controllers/eventstream.rb:12:in `on_open'
Iodine caught an unprotected exception - TypeError: wrong argument type String (expected Hash)
/usr/src/app/app/controllers/eventstream.rb:26:in `publish'
/usr/src/app/app/controllers/eventstream.rb:26:in `on_close'
8 changes: 5 additions & 3 deletions karafka.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
require_relative 'app/consumers/sections_consumer'
require_relative 'app/consumers/courses_consumer'


# Ruby on Rails setup
# Remove whole non-Rails setup that is above and uncomment the 4 lines below
# ENV['RAILS_ENV'] ||= 'development'
Expand All @@ -29,7 +28,6 @@ class KarafkaApp < Karafka::App
# config.logger = Rails.logger
end


after_init do |config|
end

Expand All @@ -45,4 +43,8 @@ class KarafkaApp < Karafka::App
end
end
end
KarafkaApp.boot!




KarafkaApp.boot!
Loading

0 comments on commit ff77fb0

Please sign in to comment.