在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:pusher/pusher-http-ruby开源软件地址:https://github.com/pusher/pusher-http-ruby开源编程语言:Ruby 100.0%开源软件介绍:Gem for Pusher ChannelsThis Gem provides a Ruby interface to the Pusher HTTP API for Pusher Channels. Supported Platforms
Installation and ConfigurationAdd gem 'pusher' or install via gem gem install pusher After registering at Pusher, configure your Channels app with the security credentials. Instantiating a Pusher Channels clientCreating a new Pusher Channels require 'pusher'
pusher = Pusher::Client.new(
app_id: 'your-app-id',
key: 'your-app-key',
secret: 'your-app-secret',
cluster: 'your-app-cluster',
use_tls: true
) The If you want to set a custom require 'pusher'
pusher = Pusher::Client.new(
app_id: 'your-app-id',
key: 'your-app-key',
secret: 'your-app-secret',
host: 'your-app-host'
) If you pass both Finally, if you have the configuration set in an pusher = Pusher::Client.from_env Global configurationThe library can also be configured globally on the Pusher.app_id = 'your-app-id'
Pusher.key = 'your-app-key'
Pusher.secret = 'your-app-secret'
Pusher.cluster = 'your-app-cluster' Global configuration will automatically be set from the If you need to make requests via a HTTP proxy then it can be configured Pusher.http_proxy = 'http://(user):(password)@(host):(port)' By default API requests are made over HTTPS. HTTP can be used by setting Pusher.use_tls = false As of version 0.12, SSL certificates are verified when using the synchronous http client. If you need to disable this behaviour for any reason use: Pusher.default_client.sync_http_client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE Interacting with the Channels HTTP APIThe Handling errorsHandle errors by rescuing begin
pusher.trigger('a_channel', 'an_event', :some => 'data')
rescue Pusher::Error => e
# (Pusher::AuthenticationError, Pusher::HTTPError, or Pusher::Error)
end LoggingErrors are logged to Pusher.logger = Rails.logger Publishing eventsAn event can be published to one or more channels (limited to 10) in one API call: pusher.trigger('channel', 'event', foo: 'bar')
pusher.trigger(['channel_1', 'channel_2'], 'event_name', foo: 'bar') An optional fourth argument may be used to send additional parameters to the API, for example to exclude a single connection from receiving the event. pusher.trigger('channel', 'event', {foo: 'bar'}, {socket_id: '123.456'}) BatchesIt's also possible to send multiple events with a single API call (max 10 events per call on multi-tenant clusters): pusher.trigger_batch([
{channel: 'channel_1', name: 'event_name', data: { foo: 'bar' }},
{channel: 'channel_1', name: 'event_name', data: { hello: 'world' }}
]) Deprecated publisher APIMost examples and documentation will refer to the following syntax for triggering an event: Pusher['a_channel'].trigger('an_event', :some => 'data') This will continue to work, but has been replaced by Getting information about the channels in your Pusher Channels appThis gem provides methods for accessing information from the Channels HTTP API. The documentation also shows an example of the responses from each of the API endpoints. The following methods are provided by the gem.
Asynchronous requestsThere are two main reasons for using the
Asynchronous calls are supported either by using an event loop (eventmachine, preferred), or via a thread. The following methods are available (in each case the calling interface matches the non-async version):
It is of course also possible to make calls to the Channels HTTP API via a job queue. This approach is recommended if you're sending a large number of events. With EventMachine
The pusher.get_async("/channels").callback { |response|
# use reponse[:channels]
}.errback { |error|
# error is an instance of Pusher::Error
} A HTTP error or an error response from Channels will cause the errback to be called with an appropriate error object. Without EventMachineIf the EventMachine reactor is not running, async requests will be made using threads (managed by the httpclient gem). An Authenticating subscription requestsIt's possible to use the gem to authenticate subscription requests to private or presence channels. The Private channelspusher.authenticate('private-my_channel', params[:socket_id]) Presence channelsThese work in a very similar way, but require a unique identifier for the user being authenticated, and optionally some attributes that are provided to clients via presence events: pusher.authenticate('presence-my_channel', params[:socket_id],
user_id: 'user_id',
user_info: {} # optional
) Receiving WebHooksA WebHook object may be created to validate received WebHooks against your app credentials, and to extract events. It should be created with the webhook = pusher.webhook(request)
if webhook.valid?
webhook.events.each do |event|
case event["name"]
when 'channel_occupied'
puts "Channel occupied: #{event["channel"]}"
when 'channel_vacated'
puts "Channel vacated: #{event["channel"]}"
end
end
render text: 'ok'
else
render text: 'invalid', status: 401
end End-to-end encryptionThis library supports end-to-end encrypted channels. This means that only you and your connected clients will be able to read your messages. Pusher cannot decrypt them. You can enable this feature by following these steps:
Important note: This will not encrypt messages on channels that are not prefixed by Limitation: you cannot trigger a single event on multiple channels in a call to pusher.trigger(
['channel-1', 'private-encrypted-channel-2'],
'test_event',
{ message: 'hello world' },
) Rationale: the methods in this library map directly to individual Channels HTTP API requests. If we allowed triggering a single event on multiple channels (some encrypted, some unencrypted), then it would require two API requests: one where the event is encrypted to the encrypted channels, and one where the event is unencrypted for unencrypted channels. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论