Skip to content

Commit 1183990

Browse files
author
Alfonso (the fonz) de la Osa
committed
initial commit
0 parents  commit 1183990

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+3271
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
node_modules

README.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
Sharing session across different platforms
2+
================
3+
4+
This experiment is intended to demonstrate the ease to share sessions between different technologies under the same umbrella (single login).
5+
6+
There are 2 different approaches: Using encripted cookies with the session information and using redis servers to store the sessions.
7+
8+
The load is balanced. The load banlancer decides if the request looks like a webpage and sends to a Ruby on Rails server or if the request looks like a WebSocket and sends it to a Node.js server.
9+
10+
TODO: Send the WebSocket request to the most available server instead of to the next in the round robin. Store the location of each connected user in a key/value storage to be able to push back to the particular server instead of broadcasting to all of them.
11+
12+
Setup
13+
-------
14+
15+
You can set up this environment using cookies or a session storage db (Redis).
16+
17+
You need node-rails-cookies package:
18+
```bash
19+
npm install node-rails-cookies
20+
```
21+
22+
###Cookies###
23+
24+
railscookies/config/initializers/session_store.rb
25+
```ruby
26+
Railscookies::Application.config.session_store :cookie_store, key: '_response_session'
27+
```
28+
29+
websockets/index.js line 16
30+
```javascript
31+
var session_store = 'cookies';
32+
```
33+
34+
###Redis###
35+
36+
railscookies/config/initializers/session_store.rb
37+
```ruby
38+
Railscookies::Application.config.session_store :redis_store_json,
39+
key: "_response_session",
40+
strategy: :json_session,
41+
domain: :all,
42+
servers: {
43+
host: :localhost,
44+
port: 6379
45+
}
46+
```
47+
48+
websockets/index.js line 16
49+
```javascript
50+
var session_store = 'redis';
51+
```
52+
53+
Running
54+
-------
55+
56+
```sh
57+
cd railscookies
58+
rails server
59+
60+
cd websockets
61+
node index.js
62+
63+
haproxy -f haproxy.conf
64+
```

haproxy.conf

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
defaults
2+
mode http
3+
timeout connect 5s
4+
timeout client 5s
5+
timeout server 60s
6+
7+
frontend proxy
8+
# listen on 80
9+
bind 0.0.0.0:80
10+
11+
# allow for many connections, with long timeout
12+
maxconn 200000 # total maximum connections, check ulimit as well
13+
timeout client 24h
14+
15+
# default to webapp backend
16+
default_backend webapp
17+
18+
# is this a socket io request?
19+
acl is_websocket path_beg /socket.io
20+
acl is_websocket hdr(Upgrade) -i WebSocket AND hdr_beg(Host) -i ws
21+
use_backend websocket if is_websocket
22+
23+
backend webapp
24+
balance roundrobin # assuming that you don't need stickiness
25+
# allow client connections to linger for 5s
26+
# but close server side requests to avoid keeping idle connections
27+
option http-server-close
28+
server ruby1 localhost:3000 check
29+
30+
backend websocket
31+
balance source # balance based on source IP
32+
33+
# options
34+
option forwardfor # add X-Forwarded-For
35+
36+
# Do not use httpclose (= client and server
37+
# connections get closed), since it will close
38+
# Websockets connections
39+
no option httpclose
40+
41+
# Use "option http-server-close" to preserve
42+
# client persistent connections while handling
43+
# every incoming request individually, dispatching
44+
# them one after another to servers, in HTTP close mode
45+
option http-server-close
46+
option forceclose
47+
48+
# timeouts are long
49+
timeout queue 5s
50+
timeout server 24h
51+
timeout connect 24h
52+
# just one node server at :8000
53+
server node1 localhost:8080 check

railscookies/.gitignore

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# See http://help.github.com/ignore-files/ for more about ignoring files.
2+
#
3+
# If you find yourself ignoring temporary files generated by your text editor
4+
# or operating system, you probably want to add a global ignore instead:
5+
# git config --global core.excludesfile '~/.gitignore_global'
6+
7+
# Ignore bundler config.
8+
/.bundle
9+
10+
# Ignore the default SQLite database.
11+
/db/*.sqlite3
12+
/db/*.sqlite3-journal
13+
14+
# Ignore all logfiles and tempfiles.
15+
/log/*.log
16+
/tmp

railscookies/Gemfile

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
source 'https://rubygems.org'
2+
3+
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
4+
gem 'rails', '4.0.0'
5+
6+
# Use SCSS for stylesheets
7+
gem 'sass-rails', '~> 4.0.0'
8+
# Use Uglifier as compressor for JavaScript assets
9+
gem 'uglifier', '>= 1.3.0'
10+
11+
# Use CoffeeScript for .js.coffee assets and views
12+
gem 'coffee-rails', '~> 4.0.0'
13+
14+
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
15+
# gem 'therubyracer', platforms: :ruby
16+
17+
# Use jquery as the JavaScript library
18+
gem 'jquery-rails'
19+
20+
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
21+
gem 'turbolinks'
22+
23+
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
24+
gem 'jbuilder', '~> 1.2'
25+
26+
group :doc do
27+
# bundle exec rake doc:rails generates the API under doc/api.
28+
gem 'sdoc', require: false
29+
end
30+
31+
gem 'therubyracer', require: "v8"
32+
33+
gem 'mongoid', git: 'git://github.com/mongoid/mongoid.git'
34+
35+
gem 'bson_ext'
36+
37+
# Devise - Authentication
38+
gem 'devise', github: 'plataformatec/devise'
39+
40+
gem 'redis-actionpack-json', github: 'nathantsoi/redis-store-json'
41+
gem 'redis-store-json', github: 'nathantsoi/redis-store-json'
42+
gem 'redis-rack-json', github: 'nathantsoi/redis-store-json'
43+
44+
# Use ActiveModel has_secure_password
45+
# gem 'bcrypt-ruby', '~> 3.0.0'
46+
47+
# Use unicorn as the app server
48+
# gem 'unicorn'
49+
50+
# Use Capistrano for deployment
51+
# gem 'capistrano', group: :development
52+
53+
# Use debugger
54+
# gem 'debugger', group: [:development, :test]

railscookies/Gemfile.lock

+174
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
GIT
2+
remote: git://github.com/mongoid/mongoid.git
3+
revision: 5d8397e24b673724f4b70ca2923a62bfe20e6e79
4+
specs:
5+
mongoid (4.0.0)
6+
activemodel (~> 4.0.0)
7+
moped (~> 1.5)
8+
origin (~> 1.0)
9+
tzinfo (~> 0.3.22)
10+
11+
GIT
12+
remote: git://github.com/nathantsoi/redis-store-json.git
13+
revision: e0d020d8e91a9c8c2850cc972ec4b20975568c55
14+
specs:
15+
redis-actionpack-json (4.0.0)
16+
actionpack (~> 4.0.0.rc1)
17+
redis-rack-json (~> 1.5.2)
18+
redis-store-json (~> 3.0.0)
19+
redis-rack-json (1.5.2)
20+
rack (~> 1.5.2)
21+
redis-store-json (~> 3.0.0)
22+
redis-store-json (3.0.0)
23+
redis (~> 3.0.0)
24+
25+
GIT
26+
remote: git://github.com/plataformatec/devise.git
27+
revision: b46b7e37360413e6a9faf43df76ffc6f6e749058
28+
specs:
29+
devise (3.0.0)
30+
bcrypt-ruby (~> 3.0)
31+
orm_adapter (~> 0.1)
32+
railties (>= 3.2.6, < 5)
33+
warden (~> 1.2.3)
34+
35+
GEM
36+
remote: https://rubygems.org/
37+
specs:
38+
actionmailer (4.0.0)
39+
actionpack (= 4.0.0)
40+
mail (~> 2.5.3)
41+
actionpack (4.0.0)
42+
activesupport (= 4.0.0)
43+
builder (~> 3.1.0)
44+
erubis (~> 2.7.0)
45+
rack (~> 1.5.2)
46+
rack-test (~> 0.6.2)
47+
activemodel (4.0.0)
48+
activesupport (= 4.0.0)
49+
builder (~> 3.1.0)
50+
activerecord (4.0.0)
51+
activemodel (= 4.0.0)
52+
activerecord-deprecated_finders (~> 1.0.2)
53+
activesupport (= 4.0.0)
54+
arel (~> 4.0.0)
55+
activerecord-deprecated_finders (1.0.3)
56+
activesupport (4.0.0)
57+
i18n (~> 0.6, >= 0.6.4)
58+
minitest (~> 4.2)
59+
multi_json (~> 1.3)
60+
thread_safe (~> 0.1)
61+
tzinfo (~> 0.3.37)
62+
arel (4.0.0)
63+
atomic (1.1.10)
64+
bcrypt-ruby (3.1.1)
65+
bson (1.9.1)
66+
bson_ext (1.9.1)
67+
bson (~> 1.9.1)
68+
builder (3.1.4)
69+
coffee-rails (4.0.0)
70+
coffee-script (>= 2.2.0)
71+
railties (>= 4.0.0.beta, < 5.0)
72+
coffee-script (2.2.0)
73+
coffee-script-source
74+
execjs
75+
coffee-script-source (1.6.3)
76+
erubis (2.7.0)
77+
execjs (1.4.0)
78+
multi_json (~> 1.0)
79+
hike (1.2.3)
80+
i18n (0.6.4)
81+
jbuilder (1.4.2)
82+
activesupport (>= 3.0.0)
83+
multi_json (>= 1.2.0)
84+
jquery-rails (3.0.4)
85+
railties (>= 3.0, < 5.0)
86+
thor (>= 0.14, < 2.0)
87+
json (1.8.0)
88+
libv8 (3.11.8.17)
89+
mail (2.5.4)
90+
mime-types (~> 1.16)
91+
treetop (~> 1.4.8)
92+
mime-types (1.23)
93+
minitest (4.7.5)
94+
moped (1.5.0)
95+
multi_json (1.7.7)
96+
origin (1.1.0)
97+
orm_adapter (0.4.0)
98+
polyglot (0.3.3)
99+
rack (1.5.2)
100+
rack-test (0.6.2)
101+
rack (>= 1.0)
102+
rails (4.0.0)
103+
actionmailer (= 4.0.0)
104+
actionpack (= 4.0.0)
105+
activerecord (= 4.0.0)
106+
activesupport (= 4.0.0)
107+
bundler (>= 1.3.0, < 2.0)
108+
railties (= 4.0.0)
109+
sprockets-rails (~> 2.0.0)
110+
railties (4.0.0)
111+
actionpack (= 4.0.0)
112+
activesupport (= 4.0.0)
113+
rake (>= 0.8.7)
114+
thor (>= 0.18.1, < 2.0)
115+
rake (10.1.0)
116+
rdoc (3.12.2)
117+
json (~> 1.4)
118+
redis (3.0.4)
119+
ref (1.0.5)
120+
sass (3.2.9)
121+
sass-rails (4.0.0)
122+
railties (>= 4.0.0.beta, < 5.0)
123+
sass (>= 3.1.10)
124+
sprockets-rails (~> 2.0.0)
125+
sdoc (0.3.20)
126+
json (>= 1.1.3)
127+
rdoc (~> 3.10)
128+
sprockets (2.10.0)
129+
hike (~> 1.2)
130+
multi_json (~> 1.0)
131+
rack (~> 1.0)
132+
tilt (~> 1.1, != 1.3.0)
133+
sprockets-rails (2.0.0)
134+
actionpack (>= 3.0)
135+
activesupport (>= 3.0)
136+
sprockets (~> 2.8)
137+
therubyracer (0.11.4)
138+
libv8 (~> 3.11.8.12)
139+
ref
140+
thor (0.18.1)
141+
thread_safe (0.1.0)
142+
atomic
143+
tilt (1.4.1)
144+
treetop (1.4.14)
145+
polyglot
146+
polyglot (>= 0.3.1)
147+
turbolinks (1.3.0)
148+
coffee-rails
149+
tzinfo (0.3.37)
150+
uglifier (2.1.1)
151+
execjs (>= 0.3.0)
152+
multi_json (~> 1.0, >= 1.0.2)
153+
warden (1.2.3)
154+
rack (>= 1.0)
155+
156+
PLATFORMS
157+
ruby
158+
159+
DEPENDENCIES
160+
bson_ext
161+
coffee-rails (~> 4.0.0)
162+
devise!
163+
jbuilder (~> 1.2)
164+
jquery-rails
165+
mongoid!
166+
rails (= 4.0.0)
167+
redis-actionpack-json!
168+
redis-rack-json!
169+
redis-store-json!
170+
sass-rails (~> 4.0.0)
171+
sdoc
172+
therubyracer
173+
turbolinks
174+
uglifier (>= 1.3.0)

railscookies/README.rdoc

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
== README
2+
3+
This README would normally document whatever steps are necessary to get the
4+
application up and running.
5+
6+
Things you may want to cover:
7+
8+
* Ruby version
9+
10+
* System dependencies
11+
12+
* Configuration
13+
14+
* Database creation
15+
16+
* Database initialization
17+
18+
* How to run the test suite
19+
20+
* Services (job queues, cache servers, search engines, etc.)
21+
22+
* Deployment instructions
23+
24+
* ...
25+
26+
27+
Please feel free to use a different markup language if you do not plan to run
28+
<tt>rake doc:app</tt>.

0 commit comments

Comments
 (0)