#!/usr/bin/env ruby -I ../lib -I lib # frozen_string_literal: true require_relative 'rainbows' require 'sinatra' set :server, :rainbows connections = [] get '/' do halt erb(:login) unless params[:user] erb :chat, locals: { user: params[:user].gsub(/\W/, '') } end get '/stream', provides: 'text/event-stream' do stream :keep_open do |out| connections << out out.callback { connections.delete(out) } end end post '/' do connections.each { |out| out << "data: #{params[:msg]}\n\n" } 204 # response without entity body end __END__ @@ layout Super Simple Chat with Sinatra <%= yield %> @@ login
@@ chat