Giter Site home page Giter Site logo

chitchat's Introduction

chitchat

Erlang Chat app

Send a message from the router to the router:

P = message_router:start().
P ! {send_chat_msg, P, "Hello!"}.

Send a message from a client to the router:

chat_client:send_message(P, "Hello from client").

Send a message from a client to another router:

P1 = chat_client:start_router().
chat_client:send_message(P1, "Hello from client to router #1").

Stop message router:

message_router:stop(P).

Conversation:

P1 = chat_client:start_router().
  <0.136.0>
P2 = chat_client:start_router().
  <0.138.0>
chat_client:send_message(P2, "Hello P2!").
  Received "Hello P2!"
  {send_chat_msg,<0.138.0>,"Hello P2!"}
chat_client:send_message(P1, "How are you P1?").
  Received "How are you P1?"
  {send_chat_msg,<0.136.0>,"How are you P1?"}
message_router:stop(P1).
  Shutting down
  shutdown
message_router:stop(P2).
  Shutting down
  shutdown

Nickname registration:

chat_client:start_router().
  true
chat_client:register_nickname("Alice").
  {register_nick,"Alice",#Fun<chat_client.0.124345407>}
chat_client:send_message("Alice", "Hello Alice").
  "Alice" received: "Hello Alice"
  {send_chat_msg,"Alice","Hello Alice"}
chat_client:send_message("Alice", "Bye Alice").
  "Alice" received: "Bye Alice"
  {send_chat_msg,"Alice","Bye Alice"}
chat_client:unregister_nickname("Alice").
  {unregister_nick,"Alice"}
chat_client:send_message("Alice", "Bye Alice").
  Unknown client
  {send_chat_msg,"Alice","Bye Alice"}

Multi-node chat:

  • Start two different nodes:
erl -sname node1 -setcookie chitchat
erl -sname node2 -setcookie chitchat
  • let's test if the nodes see each other:
(node1@imac-de-julien)1> net_adm:ping('node2@imac-de-julien').
  pong
(node2@imac-de-julien)1> net_adm:ping('node1@imac-de-julien').
  pong
  • node1 will be the message router:
(node1@imac-de-julien)3> chat_client:start_router().
  yes
  • node2 can see the registered message_router:
(node2@imac-de-julien)2> global:registered_names().
  [message_router]
  • register a nickname 'Alice' on node1:
(node1@imac-de-julien)4> chat_client:register_nickname("Alice").
  <0.49.0>
  • register a nickname 'Bob' on node2:
(node2@imac-de-julien)4> chat_client:register_nickname("Bob").
  <5898.49.0>
  • Alice sends a message to Bob:
(node1@imac-de-julien)5> chat_client:send_message("Bob", "Hi Bob!").
  <0.49.0>
  • On Bob's side:
"Bob" received: "Hi Bob!"
  • Bob replies:
(node2@imac-de-julien)4> chat_client:send_message("Alice", "How R U Alice?").
  <5898.49.0>
  • On Alice's side:
"Alice" received: "How R U Alice?"
  • Now test unregistering Alice:
(node1@imac-de-julien)7> chat_client:unregister_nickname("Alice").
  <0.49.0>
  • Bob tries to send a message to Alice again:
(node2@imac-de-julien)5> chat_client:send_message("Alice", "U there?").
  <5898.49.0>
  • On node1 (the message router):
Error: Unknown client: "Alice"

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.