Skip to content

Instantly share code, notes, and snippets.

@uu59
Created August 13, 2012 08:12
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save uu59/3338127 to your computer and use it in GitHub Desktop.
Save uu59/3338127 to your computer and use it in GitHub Desktop.
socket.io and Sinatra
vendor/
.bundle/
node_modules/
Gemfile.lock
$ bundle install
$ npm install
$ foreman start
# -- 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["websocket_port"] %>/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('http://localhost:<%= BRIDGE["websocket_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 :rubygems
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, "0.0.0.0");
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, "0.0.0.0");
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);
@shime
Copy link

shime commented Oct 5, 2013

Nifty! ❤️

Make sure to change BRIDGE["websocket_port"] to BRIDGE["stream_port"] or check out my working fork here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment