Skip to content

Commit

Permalink
Prepare for good publication
Browse files Browse the repository at this point in the history
  • Loading branch information
fxfactorial committed Oct 17, 2015
1 parent 1480a25 commit 6b1001e
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 16 deletions.
74 changes: 67 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,67 @@
These are [js\_of\_ocaml](https://github.com/ocsigen/js_of_ocaml) bindings to [nodejs](https://github.com/nodejs/node)

Get all the power of the amazing `node` ecosystem while still getting
the sanity and type safety of `OCaml`.
Get all the power of the amazing `node` ecosystem with the sanity and
type safety of `OCaml`.

The examples directory contains a chat\_server tutorial/example. To
actually have that up and running, you'll need to have `js_of_ocaml`
bindings to [socket.io](https://github.com/socketio/socket.io), those are located [here](https://github.com/fxfactorial/ocaml-npm-socket-io)
Working Chat Server
![img](./node_server_working.gif)

Here's the example's source code: which is located along side its
dependencies and make file in the `examples` directory.

```ocaml
(* Basically a translation of
http://arminboss.de/2013/tutorial-how-to-create-a-basic-chat-with-node-js/ *)
let program () =
let http = Nodejs.Http.require () in
let fs = Nodejs.Fs.require () in
let io = Socket_io.require () in
let port = 8080 in
let headers = Nodejs.js_object_of_alist [("Content-Type", "text/html")] in
let server =
http##createServer (Js.wrap_callback begin
fun request response ->
fs##readFile (Js.string "./client.html")
(Js.wrap_callback begin fun error raw_data ->
response##writeHead 200 headers;
response##end_data raw_data
end)
end)
in
let app = server##listen port
begin Js.wrap_callback begin fun () ->
Printf.sprintf
"\n\nStarted Server on local host port %d, node version: %s"
port
Nodejs.version
|> print_endline
end
end
in
let io = io##listen app in
(* Gives back a namespace object *)
io##.sockets##on
(Js.string "connection")
(* And now we get a socket *)
(Js.wrap_callback begin fun socket ->
socket##on
(Js.string "message_to_server")
(* For which we have some data, its an object *)
(Js.wrap_callback begin fun data ->
let innard = Js.Unsafe.get data "message" in
io##.sockets##emit
(Js.string "message_to_client")
(Nodejs.js_object_of_alist [("message", innard)])
end)
end)
let run p =
ignore (p ())
let () =
run program
```

# Steps to get the example working

Expand All @@ -21,15 +77,16 @@ $ cd ocaml-nodejs
$ opam pin add nodejs . -y
```

1. Get the `socket_io` package installed on your machine.
2. Get the `socket_io` package installed on your machine.

```shell
$ git clone https://github.com/fxfactorial/ocaml-npm-socket-io
$ cd ocaml-npm-socket-io
$ opam pin add socket_io . -y
```

1. Compile `chat_server.ml` into a working `node` program
2. Compile `chat_server.ml` into a working `node` program. Note that
this will install a local node module, the `socket.io` module.

```shell
$ cd examples
Expand All @@ -38,6 +95,9 @@ $ make

and open up localhost:8080, you'll have a working `node` server.

(Note that you'll only need to call `make` once, afterwards you can
directly just invoke node with `node chat_server.js`.)

# Issues

1. `node` has a pretty big API so its going to take me a little bit of
Expand Down
72 changes: 64 additions & 8 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,67 @@

These are [[https://github.com/ocsigen/js_of_ocaml][js_of_ocaml]] bindings to [[https://github.com/nodejs/node][nodejs]]

Get all the power of the amazing ~node~ ecosystem while still getting
the sanity and type safety of ~OCaml~.
Get all the power of the amazing ~node~ ecosystem with the sanity and
type safety of ~OCaml~.

The examples directory contains a chat_server tutorial/example. To
actually have that up and running, you'll need to have ~js_of_ocaml~
bindings to [[https://github.com/socketio/socket.io][socket.io]], those are located [[https://github.com/fxfactorial/ocaml-npm-socket-io][here]]
Working Chat Server
[[./node_server_working.gif]]

Here's the example's source code: which is located along side its
dependencies and make file in the ~examples~ directory.
#+BEGIN_SRC ocaml
(* Basically a translation of
http://arminboss.de/2013/tutorial-how-to-create-a-basic-chat-with-node-js/ *)
let program () =
let http = Nodejs.Http.require () in
let fs = Nodejs.Fs.require () in
let io = Socket_io.require () in

let port = 8080 in
let headers = Nodejs.js_object_of_alist [("Content-Type", "text/html")] in
let server =
http##createServer (Js.wrap_callback begin
fun request response ->
fs##readFile (Js.string "./client.html")
(Js.wrap_callback begin fun error raw_data ->
response##writeHead 200 headers;
response##end_data raw_data
end)
end)
in
let app = server##listen port
begin Js.wrap_callback begin fun () ->
Printf.sprintf
"\n\nStarted Server on local host port %d, node version: %s"
port
Nodejs.version
|> print_endline
end
end
in
let io = io##listen app in
(* Gives back a namespace object *)
io##.sockets##on
(Js.string "connection")
(* And now we get a socket *)
(Js.wrap_callback begin fun socket ->
socket##on
(Js.string "message_to_server")
(* For which we have some data, its an object *)
(Js.wrap_callback begin fun data ->
let innard = Js.Unsafe.get data "message" in
io##.sockets##emit
(Js.string "message_to_client")
(Nodejs.js_object_of_alist [("message", innard)])
end)
end)

let run p =
ignore (p ())

let () =
run program
#+END_SRC

* Steps to get the example working
I assume that you have ~opam~, ~js_of_ocaml~ and of course ~node~
Expand All @@ -24,22 +79,23 @@ $ git clone https://github.com/fxfactorial/ocaml-nodejs
$ cd ocaml-nodejs
$ opam pin add nodejs . -y
#+END_SRC

2) Get the ~socket_io~ package installed on your machine.
#+BEGIN_SRC shell
$ git clone https://github.com/fxfactorial/ocaml-npm-socket-io
$ cd ocaml-npm-socket-io
$ opam pin add socket_io . -y
#+END_SRC
3) Compile ~chat_server.ml~ into a working ~node~ program
3) Compile ~chat_server.ml~ into a working ~node~ program. Note that
this will install a local node module, the ~socket.io~ module.
#+BEGIN_SRC shell
$ cd examples
$ make
#+END_SRC

and open up localhost:8080, you'll have a working ~node~ server.

(Note that you'll only need to call ~make~ once, afterwards you can
directly just invoke node with ~node chat_server.js~.)
* Issues
1) ~node~ has a pretty big API so its going to take me a little bit of
time to cover the API and the bindings that I'm also writing for
Expand Down
5 changes: 4 additions & 1 deletion examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ all:
@js_of_ocaml a.out -o chat_server.js

clean:
@rm -f chat_server.cmi chat_server.cmo chat_server.cmt a.out
@rm -f chat_server.cmi chat_server.cmo \
chat_server.cmt a.out

.PHONY:clean
2 changes: 2 additions & 0 deletions examples/chat_server.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
(* Basically a translation of
http://arminboss.de/2013/tutorial-how-to-create-a-basic-chat-with-node-js/ *)
let program () =
let http = Nodejs.Http.require () in
let fs = Nodejs.Fs.require () in
Expand Down
Binary file added node_server_working.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6b1001e

Please sign in to comment.