![Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility](https://cdn.sanity.io/images/cgdhsj6q/production/97774ea8c88cc8f4bed2766c31994ebc38116948-1664x1366.png?w=400&fit=max&auto=format)
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Ruby gem for Centrifugo real-time messaging server
Add this line to your application's Gemfile:
gem 'centrifuge'
And then execute:
$ bundle
Or install it yourself as:
$ gem install centrifuge
Travis CI configured to automatically release tagged commits to rubygems.
Before tagging, please increment version in lib/centriguge/version.rb
and use git tag like v1.1.1
.
Dont forget to git push --tags
.
Rubycent 1.0.0+ supports only Centrifugo version 1.0.0+ with single project only. If you want to use multiproject Centrifugo, please use rubycent 0.1.0
Centrifuge::Client
- is main usable class. Start with:
client = Centrifuge::Client.new(scheme: :http, host: :localhost, port: 80, secret: 'cde')
Or just:
Centrifuge.scheme = :http
Centrifuge.host = 'localhost'
Centrifuge.port = 8000
Centrifuge.secret = 'def'
There are six methods available:
Sends message to all connected users:
client.publish('teshchannel', { data: :foo })
You can also use class methods if you set all necessary config data:
Centrifuge.publish('testchannel', { data: :foo })
Unsubscribes user from channel:
client.unsubscribe('testchannel', 'user#23')
user#23
- is string identificator of user.
Disconnects user from Centrifugo:
client.disconnect('user#23')
Gets presence info of the channel:
client.presence('testchannel')
Gets message history of the channel:
client.history('test_channel')
Get active channels (with one or more subscribers):
client.channels()
Generates token for JS client:
client.token_for('testuser', '1443422448')
Where 1443422448
is UNIX timestamp seconds. You can also add user info as valid json string as third parameter:
client.token_for('testuser', '1443422448', "{}")
Generates Sign token for a Channel:
client.generate_channel_sign(params[:client], params[:channels], "{}")
Where params[:client]
is client passed during authentication and params[:channels]
can be an array of channels or a single channel. You can read more here about batching channels here JS Documentation.
You can also add user info as valid json string as third parameter:
client.generate_channel_sign(params[:client], params[:channels], "{"name": "John"}")
On server side using rails, you can authenticate private channels like so:
#routes.rb or any router lib
post 'sockets/auth'
# Auth method to authenticate private channels
#sockets_controller.rb or anywhere in your app
# client = Instance of Centrifuge initialized // check Usage section
# params[:channels] = single/array of channels
def auth
if user_signed_in? # Or use a before_filter
data = {}
sign = client.generate_channel_sign(
params[:client], params[:channels], "{}"
)
data[channel] = {
"sign": sign,
"info": "{}"
}
render :json => data
else
render :text => "Not authorized", :status => '403'
end
end
On client side initialize Centrifuge object like so:
// client_info = valid JSON string of user_info
// client_token = client.token_for (described above)
var centrifuge = new Centrifuge({
url: "http://localhost:8000/connection",
user: window.currentUser.id,
timestamp: window.currentUser.current_timestamp,
debug: true,
info: JSON.stringify(window.client_info),
token: window.client_token,
refreshEndpoint: "/sockets/refresh",
authEndpoint: "/sockets/auth",
authHeaders: {
'X-Transaction': 'WebSocket Auth',
'X-CSRF-Token': window.currentUser.form_authenticity_token
},
refreshHeaders: {
'X-Transaction': 'WebSocket Auth',
'X-CSRF-Token': window.currentUser.form_authenticity_token
}
});
centrifuge.connect();
// If you using jbuiler use raw render(template_name) and use a global window object to load the user information.
If you want to batch sign channels request to avoid multiple HTTP requests, you can use centrifuge JS client to batch channels centrifuge.startBatching(); // your code // centrifuge.stopBatching();
in one request and on the server iterate through channels and subscribe.
Read more: JS client Documentation
When you use Centrifugo server API you should sign each request to successfully authorize with the server.
# For example this is how you would encode a Publish command
commands = {
"method": "publish",
"params": {
"channel": "Test",
"data": { "name": "John Doe" }
}
}
encoded_data = MultiJson.dump(commands) # Or use to_json
sign = client.sign(encoded_data) #Sign the data
# Using HTTP party GEM interact with Server API
r = HTTParty.post(client.url.to_s, query: {"sign": sign, "data": encoded_data})
r.parsed_response
encoded_data
is a JSON string with your API command/commands. See all available commands in Server API chapter.
To use Centrifuge js client just add this line to your application.js manifest:
//= require centrifuge
If you want to use sockjs require it before centrifuge:
//= require sockjs
//= require centrifuge
Other API methods, like projects and channels management are unavailable now.
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)FAQs
Unknown package
We found that centrifuge demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.