Skip to content

Instantly share code, notes, and snippets.

@shime
Forked from uu59/.gitignore
Last active December 24, 2015 17:39
Show Gist options
  • Save shime/6836825 to your computer and use it in GitHub Desktop.
Save shime/6836825 to your computer and use it in GitHub Desktop.
socket.io and sinatra in action
vendor/
.bundle/
node_modules/
Gemfile.lock

Example of using socket.io with Sinatra.

Cool, how do I run it?

First install these guys:

Done? Now paste this into the console:

git clone https://gist.github.com/6836825.git socket-io-sinatra-example
cd socket-io-sinatra-example
bundle install
npm install
foreman start

All good? Cake is waiting for you on localhost:4444, enjoy!

# -- coding: utf-8
require "json"
require "net/http"
require "rubygems"
require "bundler/setup"
Bundler.require
use Rack::Parser, :content_types => {
"application/json" => proc {|body| JSON.parse(body) }
}
BRIDGE = JSON.parse(IO.read("bridge.json"))
set :port, BRIDGE["rack_port"]
get "/" do
erb :index
end
post "/" do
Net::HTTP.start('127.0.0.1', BRIDGE["node_port"]) do |http|
http.post("/", "triumph=false");
end
headers "Location" => "http://localhost:#{BRIDGE["rack_port"]}/"
status 302
end
post "/report" do
if params[:note] == "HUGE SUCCESS"
puts "congrats!"
end
end
__END__
@@ layout
<!DOCTYPE html>
<html lang="ja">
<head>
<title></title>
</head>
<body><%= yield %></body></html>
@@ index
<form action="/" method="post">
post to Sinatra:
<input type="submit" value="make it fail" />
</form>
websocket emit to Node:
<input type="button" id="huge_success" value="HUGE SUCCESS" />
<pre id="received"></pre>
<script src="http://localhost:<%= BRIDGE["stream_port"] %>/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:<%= BRIDGE["stream_port"] %>');
var board = document.querySelector('#received');
socket.on('news', function (data) {
board.innerHTML += data.message + "\n";
});
document.querySelector('#huge_success').addEventListener('click', function(){
socket.emit('huge success');
});
</script>
{
"rack_port": 4444,
"node_port": 4445,
"stream_port": 4446
}
source "https://rubygems.org"
gem "rack-parser"
gem "sinatra"
gem "foreman"
{
"name": "hello-stream",
"description": "hello world test app",
"version": "0.0.1",
"private": true,
"dependencies": {
"socket.io": "0.9.x"
}
}
web: ruby ./app.rb
node: node ./server.js
var fs = require("fs");
var env = JSON.parse(fs.readFileSync("bridge.json").toString());
var io = require("socket.io").listen(env.stream_port, "127.0.0.1");
var http = require("http");
var server = http.createServer();
var msg = "the cake is a lie!";
server.on('request', function(req, res){
if(req.method == "POST" && req.url == "/") {
msg = "the cake is a lie!";
}
res.writeHead(204);
res.end();
}).listen(env.node_port, "127.0.0.1");
io.sockets.on('connection', function(sock) {
console.log('new client connected');
sock.on('disconnect', function(){
console.log('disconnect');
});
sock.on('huge success', function(){
msg = "I'm still alive.";
var req = http.request({
method: "POST",
path: "/report",
host: "localhost",
port: env.rack_port,
headers: {
"Content-Type": "application/json"
}
})
req.write(JSON.stringify({note: "HUGE SUCCESS"}));
req.end();
});
});
setInterval(function(){
io.sockets.emit('news', {message: msg});
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment