Socket
Book a DemoInstallSign in
Socket

online_manager

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

online_manager

bundlerRubygems
Version
0.0.3
Version published
Maintainers
1
Created
Source

OnlineManager

Manage whether users are online when you have a regular heartbeat, coming from a websocket for example. It allows you to have frequent heartbeats, but you only update the user's status when they transition between online and offline states. I have not run any benchmarks, but this is probably faster than updating your database every few seconds with a last_online timestamp.

It runs in an eventmachine reactor, and defines a simple DSL with callbacks which are executed when a user transitions between online and offline states.

Installation

Add this line to your application's Gemfile:

gem 'online_manager'

And then execute:

$ bundle

Or install it yourself as:

$ gem install online_manager

Usage

require 'online_manager'

# You should set all the users offline when it first runs,
# since otherwise you'll be left with users marked online from when it last quit.

class OnlineConfig

  # How long since a heartbeat was last received until the user is considered offline? (in seconds)
  def timeout
    5
  end

  # All the blocks below will be called inside an eventmachine reactor.

  def setup
    # Set up any websocket server/connection to pubsub server, etc
    # This must use eventmachine, otherwise it will block the reactor.
    # Then yield(user_id) whenever you receive a heartbeat from a user.
  end

  def online(user_id)
    puts "Booster #{user_id} online"
    # Update the user to online in the database or whatever
    # This must use asynchronous i/o, otherwise it will block the reactor.
  end

  def offline(user_id)
    puts "Booster #{user_id} offline"
    # Called when a user transitions from online -> offline
  end
end

# This runs an eventmachine reactor, so the call to .run blocks.
OnlineManager.run(OnlineConfig.new)

If you already are in the context of an eventmachine reactor, or you want to run multiple of these in the same thread, you can use .setup.

EM.run do
  OnlineManager.setup(OnlineConfig.new)
end

TODO

  • Write tests for this. Its pretty simple (~ 50 lines of code) and I have it running in production, so it should be fine. Check out this for testing eventmachine.
  • Consider setting remaining users offline in an ensure block, so it always leaves the statuses in a consistent state. Could this replace setting users offline on initialization?

Contributing

FAQs

Package last updated on 12 Aug 2014

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.