litecable 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +40 -0
- data/.rubocop.yml +63 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +128 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/circle.yml +8 -0
- data/examples/sinatra/Gemfile +16 -0
- data/examples/sinatra/Procfile +3 -0
- data/examples/sinatra/README.md +33 -0
- data/examples/sinatra/anycable +18 -0
- data/examples/sinatra/app.rb +52 -0
- data/examples/sinatra/assets/app.css +169 -0
- data/examples/sinatra/assets/cable.js +584 -0
- data/examples/sinatra/assets/reset.css +223 -0
- data/examples/sinatra/bin/anycable-go +0 -0
- data/examples/sinatra/chat.rb +39 -0
- data/examples/sinatra/config.ru +28 -0
- data/examples/sinatra/views/index.slim +8 -0
- data/examples/sinatra/views/layout.slim +15 -0
- data/examples/sinatra/views/login.slim +8 -0
- data/examples/sinatra/views/resetcss.slim +224 -0
- data/examples/sinatra/views/room.slim +68 -0
- data/lib/lite_cable.rb +29 -0
- data/lib/lite_cable/anycable.rb +62 -0
- data/lib/lite_cable/channel.rb +8 -0
- data/lib/lite_cable/channel/base.rb +165 -0
- data/lib/lite_cable/channel/registry.rb +34 -0
- data/lib/lite_cable/channel/streams.rb +56 -0
- data/lib/lite_cable/coders.rb +7 -0
- data/lib/lite_cable/coders/json.rb +19 -0
- data/lib/lite_cable/coders/raw.rb +15 -0
- data/lib/lite_cable/config.rb +18 -0
- data/lib/lite_cable/connection.rb +10 -0
- data/lib/lite_cable/connection/authorization.rb +13 -0
- data/lib/lite_cable/connection/base.rb +131 -0
- data/lib/lite_cable/connection/identification.rb +88 -0
- data/lib/lite_cable/connection/streams.rb +28 -0
- data/lib/lite_cable/connection/subscriptions.rb +108 -0
- data/lib/lite_cable/internal.rb +13 -0
- data/lib/lite_cable/logging.rb +28 -0
- data/lib/lite_cable/server.rb +27 -0
- data/lib/lite_cable/server/client_socket.rb +9 -0
- data/lib/lite_cable/server/client_socket/base.rb +163 -0
- data/lib/lite_cable/server/client_socket/subscriptions.rb +23 -0
- data/lib/lite_cable/server/heart_beat.rb +50 -0
- data/lib/lite_cable/server/middleware.rb +55 -0
- data/lib/lite_cable/server/subscribers_map.rb +67 -0
- data/lib/lite_cable/server/websocket_ext/protocols.rb +45 -0
- data/lib/lite_cable/version.rb +4 -0
- data/lib/litecable.rb +2 -0
- data/litecable.gemspec +33 -0
- metadata +256 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4640e78d6f9a77cf9257cb0774ff249ec1b5f699
|
4
|
+
data.tar.gz: a2cc894fc9e355baa79d82afde6682c570ddc21c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '0588bb0b62119753f79382bfe9955f7db54a693f2625c05426bcf08f131ed8b5b10fdb1e2bd0950545e030530ecfa823383c037747e654b43e7aa650db7187cb'
|
7
|
+
data.tar.gz: 6365980a8eece62d0d092349ed583afb1a04e749577f994da5da004dfe4576b64c07edc8744680eecda8f9637d726e6c878a0d50cd281c322070adb67c36df37
|
data/.gitignore
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# Numerous always-ignore extensions
|
2
|
+
*.diff
|
3
|
+
*.err
|
4
|
+
*.orig
|
5
|
+
*.log
|
6
|
+
*.rej
|
7
|
+
*.swo
|
8
|
+
*.swp
|
9
|
+
*.vi
|
10
|
+
*~
|
11
|
+
*.sass-cache
|
12
|
+
*.iml
|
13
|
+
.idea/
|
14
|
+
|
15
|
+
# Sublime
|
16
|
+
*.sublime-project
|
17
|
+
*.sublime-workspace
|
18
|
+
|
19
|
+
# OS or Editor folders
|
20
|
+
.DS_Store
|
21
|
+
.cache
|
22
|
+
.project
|
23
|
+
.settings
|
24
|
+
.tmproj
|
25
|
+
Thumbs.db
|
26
|
+
|
27
|
+
.bundle/
|
28
|
+
log/*.log
|
29
|
+
*.gz
|
30
|
+
pkg/
|
31
|
+
spec/dummy/db/*.sqlite3
|
32
|
+
spec/dummy/db/*.sqlite3-journal
|
33
|
+
spec/dummy/tmp/
|
34
|
+
|
35
|
+
Gemfile.lock
|
36
|
+
Gemfile.local
|
37
|
+
.rspec
|
38
|
+
*.gem
|
39
|
+
tmp/
|
40
|
+
coverage/
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
AllCops:
|
2
|
+
# Include gemspec and Rakefile
|
3
|
+
Include:
|
4
|
+
- 'lib/**/*.rb'
|
5
|
+
- 'lib/**/*.rake'
|
6
|
+
- 'spec/**/*.rb'
|
7
|
+
Exclude:
|
8
|
+
- 'bin/**/*'
|
9
|
+
- 'spec/dummy/**/*'
|
10
|
+
- 'tmp/**/*'
|
11
|
+
- 'bench/**/*'
|
12
|
+
- 'lib/anycable/rpc/**/*'
|
13
|
+
DisplayCopNames: true
|
14
|
+
StyleGuideCopsOnly: false
|
15
|
+
TargetRubyVersion: 2.3
|
16
|
+
|
17
|
+
Style/AccessorMethodName:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Style/TrivialAccessors:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
Style/Documentation:
|
24
|
+
Exclude:
|
25
|
+
- 'spec/**/*.rb'
|
26
|
+
|
27
|
+
Style/ParallelAssignment:
|
28
|
+
Enabled: false
|
29
|
+
|
30
|
+
Style/StringLiterals:
|
31
|
+
Enabled: false
|
32
|
+
|
33
|
+
Style/SpaceInsideStringInterpolation:
|
34
|
+
EnforcedStyle: no_space
|
35
|
+
|
36
|
+
Style/BlockDelimiters:
|
37
|
+
Exclude:
|
38
|
+
- 'spec/**/*.rb'
|
39
|
+
|
40
|
+
Lint/AmbiguousRegexpLiteral:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
Lint/AssignmentInCondition:
|
44
|
+
Enabled: false
|
45
|
+
|
46
|
+
Metrics/MethodLength:
|
47
|
+
Exclude:
|
48
|
+
- 'spec/**/*.rb'
|
49
|
+
|
50
|
+
Metrics/LineLength:
|
51
|
+
Max: 100
|
52
|
+
Exclude:
|
53
|
+
- 'spec/**/*.rb'
|
54
|
+
|
55
|
+
Metrics/BlockLength:
|
56
|
+
Exclude:
|
57
|
+
- 'spec/**/*.rb'
|
58
|
+
|
59
|
+
Rails/Date:
|
60
|
+
Enabled: false
|
61
|
+
|
62
|
+
Rails/TimeZone:
|
63
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 palkan
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/litecable.svg)](https://rubygems.org/gems/litecable) [![Build Status](https://travis-ci.org/anycable/litecable.svg?branch=master)](https://travis-ci.org/anycable/litecable) [![Circle CI](https://circleci.com/gh/anycable/litecable/tree/master.svg?style=svg)](https://circleci.com/gh/anycable/anycable/tree/master)
|
2
|
+
|
3
|
+
# Lite Cable
|
4
|
+
|
5
|
+
Lightweight ActionCable implementation.
|
6
|
+
|
7
|
+
Contains application logic (channels, streams, broadcasting) and also (optional) Rack hijack based server (suitable only for development and test due to its simplicity).
|
8
|
+
|
9
|
+
Compatible with [AnyCable](http://anycable.io) (for production usage).
|
10
|
+
|
11
|
+
<a href="https://evilmartians.com/">
|
12
|
+
<img src="https://evilmartians.com/badges/sponsored-by-evil-martians.svg" alt="Sponsored by Evil Martians" width="236" height="54"></a>
|
13
|
+
|
14
|
+
## Examples
|
15
|
+
|
16
|
+
- [Sinatra Lite Cable Chat](https://github.com/palkan/litecable/tree/master/examples/sinatra)
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem 'litecable'
|
24
|
+
```
|
25
|
+
|
26
|
+
And then execute:
|
27
|
+
|
28
|
+
$ bundle
|
29
|
+
|
30
|
+
Or install it yourself as:
|
31
|
+
|
32
|
+
$ gem install litecable
|
33
|
+
|
34
|
+
## Usage
|
35
|
+
|
36
|
+
Please, checkout [Action Cable guides](http://guides.rubyonrails.org/action_cable_overview.html) for general information. Lite Cable aims to be compatible with Action Cable as much as possible without the loss of simplicity and _ligthness_.
|
37
|
+
|
38
|
+
You can use Action Cable javascript client without any change (precompiled version can be found [here](https://github.com/palkan/litecable/tree/master/examples/sinatra/assets/cable.js)).
|
39
|
+
|
40
|
+
Here are the differences:
|
41
|
+
|
42
|
+
- Use `LiteCable::Connection::Base` as a base class for your connection (instead of `ActionCable::Connection::Base`)
|
43
|
+
|
44
|
+
- Use `LiteCable::Channel::Base` as a base class for your channels (instead of `ActionCable::Channel::Base`)
|
45
|
+
|
46
|
+
- Use `LiteCable.broadcast` to broadcast messages (instead of `ActionCable.server.broadcast`)
|
47
|
+
|
48
|
+
- Explicitly specify channels names:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
class MyChannel < LiteCable::Channel::Base
|
52
|
+
# Use this id in your client to create subscriptions
|
53
|
+
identifier :chat
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
```js
|
58
|
+
App.cable.subscriptions.create('chat', ...)
|
59
|
+
```
|
60
|
+
|
61
|
+
### Using built-in server (middleware)
|
62
|
+
|
63
|
+
Lite Cable comes with a simple Rack middleware for development/testing usage.
|
64
|
+
To use Lite Cable server:
|
65
|
+
|
66
|
+
- Add `gem "websocket"` to your Gemfile
|
67
|
+
|
68
|
+
- Add `require "lite_cable/server"`
|
69
|
+
|
70
|
+
- Add `LiteCable::Server::Middleware` to your Rack stack, for example:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
Rack::Builder.new do
|
74
|
+
map '/cable' do
|
75
|
+
# You have to specify your app's connection class
|
76
|
+
use LiteCable::Server::Middleware, connection_class: App::Connection
|
77
|
+
run proc { |_| [200, { 'Content-Type' => 'text/plain' }, ['OK']] }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
### Using with AnyCable
|
83
|
+
|
84
|
+
Lite Cable is AnyCable-compatible out-of-the-box. You should write a simple server script:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
#!/usr/bin/env ruby
|
88
|
+
|
89
|
+
require "my_app"
|
90
|
+
require "rack"
|
91
|
+
require "anycable"
|
92
|
+
|
93
|
+
# Turn AnyCable compatibility mode
|
94
|
+
LiteCable.anycable!
|
95
|
+
|
96
|
+
Anycable.configure do |config|
|
97
|
+
config.connection_factory = MyApp::Connection
|
98
|
+
end
|
99
|
+
|
100
|
+
Anycable::Server.start
|
101
|
+
```
|
102
|
+
|
103
|
+
And then run this script along with your application. See [Sinatra example](https://github.com/palkan/litecable/tree/master/examples/sinatra) for more.
|
104
|
+
|
105
|
+
### Configuration
|
106
|
+
|
107
|
+
Lite Cable uses [anyway_config](https://github.com/palkan/anyway_config) for configuration.
|
108
|
+
|
109
|
+
See [config](https://github.com/palkan/litecable/blob/master/lib/lite_cable/config.rb) for available options.
|
110
|
+
|
111
|
+
### Unsupported features
|
112
|
+
|
113
|
+
- Channel callbacks (`after_subscribe`, etc)
|
114
|
+
|
115
|
+
- Stream callbacks (`stream_from "xyz" { |msg| ... }`)
|
116
|
+
|
117
|
+
- Periodical timers
|
118
|
+
|
119
|
+
- Remote connections.
|
120
|
+
|
121
|
+
## Contributing
|
122
|
+
|
123
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/anycable/litecable.
|
124
|
+
|
125
|
+
## License
|
126
|
+
|
127
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
128
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "litecable"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/circle.yml
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# Lite Cable Sinatra Demo
|
2
|
+
|
3
|
+
Sample chat application built with [Sinatra](http://www.sinatrarb.com) and Lite Cable.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Install dependencies:
|
8
|
+
|
9
|
+
```sh
|
10
|
+
bundle install
|
11
|
+
```
|
12
|
+
|
13
|
+
Run server:
|
14
|
+
|
15
|
+
```sh
|
16
|
+
bundle exec puma
|
17
|
+
```
|
18
|
+
|
19
|
+
Open your browser at [localhost:9292](http://localhost:9292), enter your name and a chat room ID (anything you want).
|
20
|
+
|
21
|
+
Then open another session (another browser, incognito window) and repeat all steps using the same room ID.
|
22
|
+
|
23
|
+
Now you can chat with yourself!
|
24
|
+
|
25
|
+
## AnyCable usage
|
26
|
+
|
27
|
+
This example also can be used with [AnyCable](http://anycable.io).
|
28
|
+
|
29
|
+
Just run `Procfile` with your favourite tool ([hivemind](https://github.com/DarthSim/hivemind) or [Foreman](http://ddollar.github.io/foreman/)):
|
30
|
+
|
31
|
+
```sh
|
32
|
+
hivemind
|
33
|
+
```
|
@@ -0,0 +1,18 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
lib = File.expand_path("../../../lib", __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
|
6
|
+
require "./chat"
|
7
|
+
require "rack"
|
8
|
+
require "anycable"
|
9
|
+
|
10
|
+
# Turn AnyCable compatibility mode
|
11
|
+
LiteCable.anycable!
|
12
|
+
|
13
|
+
Anycable.configure do |config|
|
14
|
+
config.connection_factory = Chat::Connection
|
15
|
+
config.debug = true
|
16
|
+
end
|
17
|
+
|
18
|
+
Anycable::Server.start
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "sinatra"
|
3
|
+
require "sinatra/cookies"
|
4
|
+
|
5
|
+
CABLE_URL = ENV['ANYCABLE'] ? "ws://localhost:9293/cable" : "/cable"
|
6
|
+
|
7
|
+
class App < Sinatra::Application # :nodoc:
|
8
|
+
set :public_folder, 'assets'
|
9
|
+
|
10
|
+
enable :sessions
|
11
|
+
set :session_secret, 'my_secrets'
|
12
|
+
|
13
|
+
get '/' do
|
14
|
+
if session[:user]
|
15
|
+
slim :index
|
16
|
+
else
|
17
|
+
slim :login
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
get '/sign_in' do
|
22
|
+
slim :login
|
23
|
+
end
|
24
|
+
|
25
|
+
post '/sign_in' do
|
26
|
+
if params['user']
|
27
|
+
session[:user] = params['user']
|
28
|
+
cookies["user"] = params['user']
|
29
|
+
redirect '/'
|
30
|
+
else
|
31
|
+
slim :login
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
post '/rooms' do
|
36
|
+
if params['id']
|
37
|
+
redirect "/rooms/#{params['id']}"
|
38
|
+
else
|
39
|
+
slim :index
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
get '/rooms/:id' do
|
44
|
+
if session[:user]
|
45
|
+
@room_id = params['id']
|
46
|
+
@user = session[:user]
|
47
|
+
slim :room
|
48
|
+
else
|
49
|
+
slim :login
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|