elixir-phx-websocket-chat開発メモ
プロジェクト名
elixir-phx-websocket-chat
練習だしこれでいいや
コンセプト
Elixir / PhoenixでWebSocketを使ったWebアプリケーションの練習として。とりあえず同時にアクセスしている人すべてに発言が届くようであればOK。
Dockerでポータビリティをもたせることは可能か
WebSocketを利用可能か。またその性能はどの程度か
フロントエンドをyarn、バックエンドをmixのプロジェクトとして開発し、統合可能か
RPC実装は可能か(JSON-RPC, MessagePack-RPC, gRPCなど)
WebpackでVueアプリケーションをビルドするにはどうすればよいか
特徴
SPA (Vue.js)
WebSocketによるリアルタイムチャット
部屋は1つだけ
調査
Docker
FrontEnd
WebSocket
どうやらクライアント側ではPhoenixで用意されたライブラリを使わないといけない模様
CowboyとPlugでなんとかできそうなのでPhoenixで用意されたものは使わないで対応する事も考える
MessagePack
MessagePack-RPC
MessagePack-RPCのElixir実装はなさそう
gRPC
なるほどね、完全に理解した。
開発環境
バージョン
code:shell-session
$ mix hex.info
Hex: 0.17.7
Elixir: 1.6.5
OTP: 20.3
mix phx.new
code:sh
ghq get git@github.com:c18t/elixir-phx-websocket-chat.git
ghq look git@github.com:c18t/elixir-phx-websocket-chat.git
mix phx.new . --app websocket_chat --no-brunch --no-ecto
mix release.init
code:sh
mix do deps.get, compile
mix release.init
frontend
code:json
"dependencies": {
"cross-env": "^5.1.6",
"css-loader": "^0.28.11",
"extract-text-webpack-plugin": "^3.0.2",
"file-loader": "^1.1.4",
"npm-run-all": "^4.1.3",
"path": "^0.12.7",
"prettier": "^1.12.1",
"stylus": "^0.54.5",
"stylus-loader": "^3.0.2",
"ts-loader": "^3.4.0",
"typescript": "^2.8.3",
"vue": "^2.5.11",
"vue-class-component": "^6.2.0",
"vue-loader": "^15.2.1",
"vue-style-loader": "^4.1.0",
"vue-template-compiler": "^2.4.4",
"webpack": "^3.6.0"
},
"devDependencies": {
"opener": "^1.4.3",
"rimraf": "^2.6.2",
"wait-on": "^2.1.0"
}
code:sh
tsc --init
tsconfig.json
code:json
{
"include": [
"./lib/js/**/*"
],
"compilerOptions": {
/* Basic Options */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */
"module": "es2015", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
// "jsx": "preserve", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
// "declaration": true, /* Generates corresponding '.d.ts' file. */
"sourceMap": true, /* Generates corresponding '.map' file. */
// "outFile": "./", /* Concatenate and emit output to single file. */
"outDir": "./_build/js/", /* Redirect output structure to the directory. */
// "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
// "removeComments": true, /* Do not emit comments to output. */
// "noEmit": true, /* Do not emit outputs. */
// "importHelpers": true, /* Import emit helpers from 'tslib'. */
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
/* Strict Type-Checking Options */
"strict": true, /* Enable all strict type-checking options. */
// "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* Enable strict null checks. */
// "strictFunctionTypes": true, /* Enable strict checking of function types. */
// "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */
"noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */
// "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */
/* Additional Checks */
// "noUnusedLocals": true, /* Report errors on unused locals. */
// "noUnusedParameters": true, /* Report errors on unused parameters. */
"noImplicitReturns": true, /* Report error when not all code paths in function return a value. */
// "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */
/* Module Resolution Options */
"moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */
// "baseUrl": "./", /* Base directory to resolve non-absolute module names. */
// "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */
// "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */
"typeRoots": [ /* List of folders to include type definitions from. */
"./lib/js/d"
],
// "types": [], /* Type declaration files to be included in compilation. */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
// "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */
/* Source Map Options */
// "sourceRoot": "./", /* Specify the location where debugger should locate TypeScript files instead of source locations. */
// "mapRoot": "./", /* Specify the location where debugger should locate map files instead of generated locations. */
// "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */
// "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */
/* Experimental Options */
"experimentalDecorators": true /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
}
}
yarn scripts
code:sh
yarn build # webpack & mix compile
yarn watch # webpack --watch & mix phx.server
docker run
code:sh
# 最新のソースを取得
cd elixir-phx-websocket-chat
# イメージの作成
docker build -t elixir-phx-websocket-chat .
# コンテナを起動
docker run -d -p 4000:4000 elixir-phx-websocket-chat
# ブラウザを開く
リリースビルドの配布とかしないしDockerfile内でソースダウンロードするのいろいろ拙いのでgit cloneでソースを取得するようにした。
課題について
Dockerでポータビリティをもたせることは可能か
可能。Dockerfileでビルドさせることができた。リリースパッケージもできたので、iex?さえあれば動くようにもできるはず。
WebSocketを利用可能か。またその性能はどの程度か
利用可能。ただし、Phoenixで用意されたjsに依存するようになるため、拡張が難しいかもしれない。例えば、MessagePack化。
フロントエンドをyarn、バックエンドをmixのプロジェクトとして開発し、統合可能か
可能。yarn scriptでビルド、ウォッチ、サーバー起動、リリースビルドのすべてが問題なく動作した。
RPC実装は可能か(JSON-RPC, MessagePack-RPC, gRPCなど)
検討中。
webpackでVueアプリケーションをビルドするにはどうすればよいか
vue-loaderで.vueファイルを処理する。14系以前と15以降でvue-loaderの仕様が大きく変わっているので注意。
メモ
mixで困ったら
code:sh
mix help
bashでインラインコメント
code:sh
$ echo "Hello, " && : "this is comment!" && echo "world!"
Hello,
world!
fishだと何もしないコマンドがないのでエラーを無視するしかない…
code:fish(sh)
$ echo "Hello, "; and true "this is comment!" >/dev/null 2>&1; echo "world!"
Hello,
world!
Q: なにがうれしいの? A: #の行末コメントと違って改行ができる
build中のコマンドの実行結果を環境変数として登録する
1. RUN echo 'export HOGE="(date)"' >> ~/.bashrcのように、bashのソースファイルを作成する
2. ENV BASH_ENV=~/.bashrcで環境変数BASH_ENVを指定し、bash実行時にソースファイルが読まれるようにする
3. ENTRYPOINT [ "/bin/bash", "-c" ]でbash経由でコマンドが実行されるようにする。このときJSON形式にしておく。(docker run時にcmd渡せるようにするため)
4. CMD [ "echo $HOGE" ]のように、環境変数を使うコマンドを指定。-cオプションの値なのでJSON形式で全体をひとつの文字列で渡す。
5. docker run image-nameでHOGEの内容が表示される
6. docker run時に値を上書きしたい場合は、次のような内容で.envファイルを作成し、--env-fileオプションに指定して渡す。
code:.env
# コンテナ内の~/.bashrcを読み込まなくなるおまじない
BASH_ENV
# HOGEを上書き
HOGE=Hello, world!
code:sh
$ docker run --env-file .env image-name