File tree Expand file tree Collapse file tree 2 files changed +84
-0
lines changed
examples/presence_channels Expand file tree Collapse file tree 2 files changed +84
-0
lines changed Original file line number Diff line number Diff line change
1
+ require 'sinatra'
2
+ require 'sinatra/cookies'
3
+ require 'sinatra/json'
4
+ require 'pusher'
5
+
6
+ # You can get these variables from http://dashboard.pusher.com
7
+ pusher = Pusher ::Client . new (
8
+ app_id : 'your-app-id' ,
9
+ key : 'your-app-key' ,
10
+ secret : 'your-app-secret' ,
11
+ cluster : 'your-app-cluster'
12
+ )
13
+
14
+ set :public_folder , 'public'
15
+
16
+ get "/" do
17
+ redirect '/presence_channels.html'
18
+ end
19
+
20
+ # Emulate rails behaviour where this information would be stored in session
21
+ get '/signin' do
22
+ cookies [ :user_id ] = 'example_cookie'
23
+ 'Ok'
24
+ end
25
+
26
+ # Auth endpoint: https://pusher.com/docs/channels/server_api/authenticating-users
27
+ post '/pusher/auth' do
28
+ channel_data = {
29
+ user_id : 'example_user' ,
30
+ user_info : {
31
+ name : 'example_name' ,
32
+ email : 'example_email'
33
+ }
34
+ }
35
+
36
+ if cookies [ :user_id ] == 'example_cookie'
37
+ response = pusher . authenticate ( params [ :channel_name ] , params [ :socket_id ] , channel_data )
38
+ json response
39
+ else
40
+ status 403
41
+ end
42
+ end
43
+
44
+ get '/pusher_trigger' do
45
+ channels = [ 'presence-channel-test' ] ;
46
+
47
+ begin
48
+ pusher . trigger ( channels , 'test-event' , {
49
+ message : 'hello world'
50
+ } )
51
+ rescue Pusher ::Error => e
52
+ # (Pusher::AuthenticationError, Pusher::HTTPError, or Pusher::Error)
53
+ end
54
+
55
+ 'Triggered!'
56
+ end
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < head >
3
+ < title > Pusher Test</ title >
4
+ < script src ="https://js.pusher.com/5.0/pusher.min.js "> </ script >
5
+ < script >
6
+
7
+ // Enable pusher logging - don't include this in production
8
+ Pusher . logToConsole = true ;
9
+
10
+ var pusher = new Pusher ( 'your-app-key' , {
11
+ cluster : 'your-app-cluster' ,
12
+ forceTLS : true ,
13
+ authEndpoint : '/pusher/auth'
14
+ } ) ;
15
+
16
+ var channel = pusher . subscribe ( 'presence-channel-test' ) ;
17
+ channel . bind ( 'test-event' , function ( data ) {
18
+ alert ( JSON . stringify ( data ) ) ;
19
+ } ) ;
20
+ </ script >
21
+ </ head >
22
+ < body >
23
+ < h1 > Pusher Test</ h1 >
24
+ < p >
25
+ Try publishing an event to channel < code > presence-channel-test</ code >
26
+ with event name < code > test-event</ code > .
27
+ </ p >
28
+ </ body >
You can’t perform that action at this time.
0 commit comments