Coder Social home page Coder Social logo

n2o's Introduction

N2O for Haskell

Build Status

Features

  • Endpoints: poor man's WebSocket and static HTTP server
  • Formatters: BERT
  • Protocols: NITRO
  • High Performance Protocol Relay
  • Smallest possible codebase — 500 LOC

N2O defines a way we scale protocols, database schema, applications and services across companies, formatters, views and presentation layers. At the core N2O folds a list of protocols and their handlers providing a minimal type-level specification for general purpose application protocol tract.

As example this Haskell version of N2O is shipped with Nitro protocol implementation, that listens the tract and push prerendered JavaScript events back to the channel. This smart and simple reactive way of client-server interaction first was used by Rusty Klophaus in his Nitrogen web framework, that was pushed forward since then in N2O by Andy Melnikov and Marat Khafizov.

Setup

stack build
stack exec n2o-sample
open http://localhost:3000/samples/static/index.html

Nitro Protocol Demo

{-# LANGUAGE FlexibleContexts, OverloadedStrings, DeriveGeneric, DeriveAnyClass #-}
module Main (main) where

import Network.N2O
import Network.N2O.Web hiding (Event)
import GHC.Generics (Generic)
import Data.Serialize (Serialize)

data Example = Greet deriving (Show, Eq, Generic, Serialize)

main = runServer "localhost" 3000 cx

cx :: Cx Example
cx = mkCx{ cxMiddleware=[router]
         , cxProtos = [nitroProto]
         }

router cx@Context{cxReq=Req{reqPath=path}} =
  let handle = case path of
                  "/ws/samples/static/index.html" -> index
                  "/ws/samples/static/about.html" -> about
                  _ -> index
  in cx{cxHandler=mkHandler handle}

index Init = do
  updateText "system" "What is your name?"
  wire button{id_="send", postback=Just Greet, source=["name"]}
index (Message Greet) = do
  Just name <- get "name" -- wf:q/1
  updateText "system" ("Hello, " <> jsEscape name <> "!")
index ev = liftIO $ putStrLn ("Unknown event" <> show ev)
about Init = updateText "app" "This is the N2O Hello World App"
about ev = liftIO $ putStrLn ("Unknown event " <> show ev)

Credits

  • Andy Melnikov
  • Marat Khafizov
  • Maxim Sokhatsky

n2o's People

Contributors

5ht avatar marikutsa avatar xafizoff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

n2o's Issues

Received ExitFailure 2 when running

$ stack exec n2o-sample
Preparing to install GHC (tinfo6) to an isolated location.
This will not interfere with any system-level installation.
Already downloaded.
Unpacking GHC into /home/roland/.stack/programs/x86_64-linux/ghc-tinfo6-8.4.3.temp/ ...
Received ExitFailure 2 when running
Raw command: /usr/bin/tar Jxf /home/roland/.stack/programs/x86_64-linux/ghc-tinfo6-8.4.3.tar.xz
Run from: /home/roland/.stack/programs/x86_64-linux/ghc-tinfo6-8.4.3.temp/

Add benchmarks

tcpkali -T20s -r 10000 -c 50 --first-message "N2O," -m PING --ws 127.0.0.1:3000/ws/static/index.html
Destination: [127.0.0.1]:3000
Interface lo address [127.0.0.1]:0
Using interface lo to connect to [127.0.0.1]:3000
Ramped up to 50 connections.
Total data sent:     95.5 MiB (100175973 bytes)
Total data received: 28.3 MiB (29665440 bytes)
Bandwidth per channel: 1.038⇅ Mbps (129.8 kBps)
Aggregate bandwidth: 11.862↓, 40.055↑ Mbps
Packet rate estimate: 23925.7↓, 40424.5↑ (1↓, 1↑ TCP MSS/op)
Test duration: 20.0078 s.

Intel(R) Core(TM) i5 CPU M 520 @ 2.40GHz
4G RAM

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.