Socket
Book a DemoInstallSign in
Socket

simple_pusher

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

simple_pusher

bundlerRubygems
Version
0.0.6
Version published
Maintainers
1
Created
Source

SimplePusher

SimplePusher is a HTML5 websocket powered realtime messaging tool used in Rails / Sintra project.

Installation

Gem Version

Add this line to your application's Gemfile:

gem 'simple_pusher'

And then execute:

$ bundle

Or install it yourself as:

$ gem install simple_pusher

Then start simple pusher server when rails app boot:

# config/initializers/simple_pusher.rb
EventMachine.next_tick do
  SimplePusher.setup do |config|
    config.port = 8088
  end
  SimplePusher.start
end

Note

As initializer code config/initializers/simple_pusher.rb show, your rails runtime environment must be in EventMachine run loop. Otherwise you start EventMachine run loop in a new thread.

Usage

Setup client js code.

Add //= require simple_pusher to app/assets/javascripts/application.js

Add code to app/views/layouts/application.html.erb

<script type="text/javascript">
    var simple_pusher = new SimplePusher("ws://<%= request.host %>:8088/");
    simple_pusher.on('channel_name', function(message){
      alert('got message:'+message);
    });
</script>

Broadcast message via server side.

SimplePusher.publish('channel_name', "Time now #{Time.now.to_s(:db}")

Broadcast message via http POST.

curl -d 'channel=channel1&message=hello'  http://localhost:3000/simple_pusher

Message callback at server side.

SimplePusher.on("ping") do
  puts "I received ping request."
end

SimplePusher.on("ping") do
  puts "I also received ping request."
end

Use SimplePusher in Sinatra app

# app.rb
require 'simple_pusher/sinatra_app'

EventMachine.next_tick do
  SimplePusher.setup do |config|
    config.port = 8088
  end
  SimplePusher.start
end

class MyApp < Sinatra::Base

  use SimplePusher::SinatraApp # With this you just setup http post interface: curl -d 'channel=channel1&message=hello'  http://localhost:3000/simple_pusher
end

Currently I have not implement directly require simple_pusher js from project. You can download simple_pusher.coffee file from Github repository to your project for use.

Emulating WebSockets in older browsers

It is possible to emulate WebSockets in older browsers using flash emulation. For example take a look at the web-socket-js project.

TODO

  • Add on method message callback to client js.
  • Support JSON format message.
  • Use SimplePusher js in Sinatra app.

Contributing

  • Fork it
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create new Pull Request

FAQs

Package last updated on 12 Jun 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.