Code Monkey home page Code Monkey logo

chatly_demo's Introduction

Chatly

To start your Phoenix server:

  • Install dependencies with mix deps.get
  • Install Node.js dependencies with cd assets && npm install
  • Start Phoenix endpoint with mix phx.server

Now you can visit localhost:4000 from your browser.

Ready to run in production? Please check our deployment guides.

Learn more

How to recreate this app

Create your app:

~ % mix phx.new chatly --no-ecto
  Fetch and install dependencies? [Yn] Y
~ % mix phx.gen.channel Room

Add jquery to /assets/package.json:

"jquery": ">= 2.1"

Replace content in /lib/chatly_web/templates/page/index.html.eex:

<div id="list" class="row"></div>

<div class="row form-group">
  <div class="col-xs-3">
    <input type="text" id="name" class="form-control" placeholder="Name">
  </div>
  <div class="col-xs-9">
    <input type="text" id="message" class="form-control" placeholder="Message">
  </div>
</div>

Add to /assets/css/app.css:

#list {
  border: 1px solid black;
  height: 300px;
  padding: 5px;
  overflow: scroll;
  margin-bottom: 30px;
}

Add to /lib/chatly_web/channels/user_socket.ex:

channel "room:*", ChatlyWeb.RoomChannel

Add to /assets/js/app.js:

import socket from "./socket"
import $ from "jquery"

let channel = socket.channel("room:lobby", {})
let list = $('#list')
let name = $('#name')
let message = $('#message')

channel.join()
  .receive("ok", resp => { console.log("Joined successfully", resp) })
  .receive("error", resp => { console.log("Unable to join", resp) })

channel.on('shout', payload => {
  list.append(`<b>${payload.name}:</b> ${payload.message}<br/>`);
});

message.on('keypress', event => {
  if (event.keyCode == 13) {
    channel.push('shout', {
      name: name.val(),
      message: message.val()
    })
    message.val('')
  }
})

Add to /lib/chatly_web/channels/room_channel.ex

def handle_info(:after_join, socket) do
  push(socket, "shout", %{name: "Chatly", message: "Welcome!"})
  {:noreply, socket}
end

Start your server!

~ % mix phx.server

Go to localhost:4000

chatly_demo's People

Watchers

James Cloos avatar

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.