Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Pull Request for Issue #7 #8 ( Bryan + Minja) #9

Merged
merged 28 commits into from
Sep 29, 2018
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
7c645e7
Added a README
bdieu178 Jul 4, 2018
084cbbe
Added initial files
bdieu178 Jul 4, 2018
e030a7f
Initial draft for Websocket
bdieu178 Jul 20, 2018
c3290c0
Unstable build for uploading sharing
bdieu178 Jul 21, 2018
8ff6750
Staging for WIP PR
bdieu178 Jul 21, 2018
101537c
Need to test
bdieu178 Jul 21, 2018
ca538a6
Testing code for appropriate functionality
bdieu178 Jul 22, 2018
4246ff4
Delete websocket.rb
bdieu178 Jul 22, 2018
3a71374
Updated staging for WIP PR
bdieu178 Jul 22, 2018
f5ad690
Merge branch 'master' of github.com:bdieu178/yacs-notifications into …
bdieu178 Jul 22, 2018
abf0d94
Update sections_consumer.rb
bdieu178 Jul 22, 2018
7eb0f74
Removed websocket/archive
bdieu178 Jul 22, 2018
dd13d80
Fixed plezi loading errors
bdieu178 Jul 24, 2018
5dcacf2
Fixed styling
bdieu178 Jul 24, 2018
2c84367
Resolved issue #7 & #8
bdieu178 Jul 25, 2018
a0f109c
Resolved issue #7 & #8, but need to format JSON output
bdieu178 Jul 25, 2018
766c3e5
Removed app/app folder
bdieu178 Jul 25, 2018
892561d
Removed duplicated app/app folder
bdieu178 Jul 25, 2018
426bb75
Fixed consumers & eventsream classes
bdieu178 Jul 25, 2018
946ee4d
Updated eventsream class
bdieu178 Jul 25, 2018
af7090a
Build up a working ws controller, not yet in Docker(add 3000 to yml f…
huangmj7 Jul 27, 2018
74ae4b7
1st Attempt to get websocket running using Iodine only
bdieu178 Jul 31, 2018
aabe503
Failed attempt, config.ru failed
huangmj7 Jul 31, 2018
4eed4ea
bdieu178's versions
huangmj7 Jul 31, 2018
e7c3ec6
Karafka consumer can now send messages to the Iodine Pub/Sub
bdieu178 Aug 7, 2018
ff77fb0
Fixed conflicts and incorporated Minja & Ada's Websocket config
bdieu178 Aug 7, 2018
7273e43
Updated Kafka consumers to push directly to Iodine pub/sub
bdieu178 Aug 7, 2018
727d8c9
Update client.js
bdieu178 Aug 7, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,9 @@ source 'https://rubygems.org'
ruby '2.5.1'
gem 'karafka'

# include the basic plezi framework and server
gem 'plezi'
## Redis servers are used to allow websocket scaling.
## Plezi can be configured to automatically use Redis for easy scaling.
# gem 'redis'
gem 'iodine', '~>0.6' # may need to fix the version
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
YACS Event Stream Websocket API
---
Implemented using Kafka & Plezi
36 changes: 34 additions & 2 deletions app.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,35 @@
require 'karafka'
# encoding: UTF-8

puts 'Hello world'
## 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) }

## 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

# load routes.
load Root.join('routes.rb').to_s
14 changes: 14 additions & 0 deletions app/consumers/courses_consumer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'karafka'
require_relative 'application_consumer'
require 'eventstream'

class CourseConsumer < ApplicationConsumer
def consume
if ( Object.const_defined?('EventStream') == false )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this check needed?

puts "EventStream class not initialized"
else
EventStream.on_message(params)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You cannot call on_message, it is called internally by plezi only when the websocket server receives a message from the client.

puts "Consumer message sent to websocket"
end
end
end
12 changes: 9 additions & 3 deletions app/consumers/sections_consumer.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
require 'karafka'
require_relative 'application_consumer'
require 'eventstream'

class SectionConsumer < ApplicationConsumer
def consume
puts params #print out the single message received
end
def consume
if ( Object.const_defined?('EventStream') == false )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, not sure why we need this check

puts "EventStream class not initialized"
else
EventStream.on_message(params)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you cannot call on_message, it is called by plezi when a websocket client sends a message to the server, where data is the contents of the message

puts "Consumer message sent to websocket"
end
end
end
18 changes: 18 additions & 0 deletions app/controllers/eventstream.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'plezi'

class EventStream

def on_open
puts 'Opened the YACS-EventStream Websocket!'
end

## When receiving a single message from Consumer pusblish to socket
def on_message(data)
unless write { notifications: :data }.to_json
write "no message sent"
end
end

def on_close
end
end
8 changes: 8 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Default Rack interface

# load the application
load ::File.expand_path(File.join('..', 'app.rb'), __FILE__)

Iodine::DEFAULT_HTTP_ARGS[:public] ||= './public'

run Plezi.app
6 changes: 5 additions & 1 deletion karafka.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# require ::File.expand_path('../config/environment', __FILE__)
# Rails.application.eager_load!
require 'karafka'
require 'app'

class KarafkaApp < Karafka::App
setup do |config|
Expand All @@ -38,6 +39,9 @@ class KarafkaApp < Karafka::App
consumer SectionConsumer #Single message from section_change
end
end
topic :course_change do
consumer CourseConsumer
end
end
end
KarafkaApp.boot!
KarafkaApp.boot!
7 changes: 7 additions & 0 deletions rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# load all application code and gems
require 'bundler'
Bundler.require(:default, ENV['ENV'].to_s.to_sym)
# set Plezi to rake mode and load it's tasks (if relevant).
Plezi.no_autostart

# Add your tasks
3 changes: 3 additions & 0 deletions routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
##
# ws://0.0.0.0/EventStream
Plezi.route '/', EventStream