Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9050487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:39:39+00:00 2026-06-16T12:39:39+00:00

I have a simple Sinatra App running on EventMachine, like this example . The

  • 0

I have a simple Sinatra App running on EventMachine, like this example.

The app is working, now I’d like to allow the routes I’m defining in Sinatra to access the websocket using the EventMachine channel that is created. I naively tried the following, but of course within the Sinatra App, the @channel variable isn’t defined, so this doesn’t work.

require 'em-websocket'
require 'sinatra'

EventMachine.run do
  @channel = EM::Channel.new

  class App < Sinatra::Base
    get '/' do
      erb :index
    end

    post '/test' do
      @channel.push "Post request hit endpoint"
      status 200
    end
  end


  EventMachine::WebSocket.start :host => '0.0.0.0', :port => 8080 do |socket|
    socket.onopen do
      sid = @channel.subscribe { |msg| socket.send msg }
      @channel.push "Subscriber ID #{sid} connected!"

      socket.onmessage do |msg|
        @channel.push "Subscriber <#{sid}> sent message: #{msg}"
      end

      socket.onclose do
        @channel.unsubscribe(sid)
      end
    end
  end

  App.run! :port => 3000
end

How could I access the EventMachine channel I’ve got open within my Sinatra app?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T12:39:40+00:00Added an answer on June 16, 2026 at 12:39 pm

    In case others don’t know what we’re talking about in the comments, here’s an example of using a class instance variable in the way I suggested. This runs, but I don’t know if it does what’s expected:

    require 'em-websocket'
    require 'sinatra'
    require 'haml'
    
    module Example
    
      def self.em_channel
        @em_channel ||= EM::Channel.new
      end
    
      EventMachine.run do
    
        class App < Sinatra::Base
          configure do
            enable :inline_templates
          end
    
          get '/' do
            haml :index
          end
    
          get '/test' do
            Example.em_channel.push "Test request hit endpoint"
            status 200
          end
        end
    
    
        EventMachine::WebSocket.start :host => '0.0.0.0', :port => 8080 do |socket|
          socket.onopen do
            sid = Example.em_channel.subscribe { |msg| socket.send msg }
            Example.em_channel.push "Subscriber ID #{sid} connected!"
    
            socket.onmessage do |msg|
              Example.em_channel.push "Subscriber <#{sid}> sent message: #{msg}"
            end
    
            socket.onclose do
              Example.em_channel.unsubscribe(sid)
            end
          end
        end
    
        App.run!
      end
    end
    
    __END__
    
    @@ layout
    %html
      = yield
    
    @@ index
    %div.title Hello world.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple Sinatra app. web.rb: require 'sinatra' get '/' do Hello end Gemfile:*
I have a .Net XML webservice that I need a simple Sinatra app to
I have a very simple sinatra site that I'm trying to access via ajax
I have a simple server in Sinatra, like require 'sinatra/base' class Server < Sinatra::Base
i have simple html5 video player like this <video id=video controls=true src=video.webm></video> If the
I have a simple Sinatra app that I am playing with, and for some
I have a fairly simple Tweetstream listener built in a Sinatra app that I
I've been trying to get a simple hello world web app running on Sinatra.
I have created a simple app and this is my folder structure lib/book.rb lib/user.rb
I am working with DataMapper and Sinatra to create a simple app. Here's the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.