WebSocket Chat

This recipe will help you to setup a basic WebSocket chat in your application.

First you need an amber project generated with Amber CLI or from scratch.

src/channels/chat_room_channel.cr
```ruby class ChatRoomChannel < Amber::Websockets::Channel def handle_message(client_socket, msg) rebroadcast!(msg) end end ```

Then setup your socket file:

src/sockets/chat_socket.cr
```ruby struct ChatSocket < Amber::WebSockets::ClientSocket channel "chat_room:*", ChatRoomChannel

def on_connect # returning true accept all connections # you can use authentication here true end end

</div>


Then add the `websocket` verb in your routes file:


<div class="code-block-titled"><div class="code-title">config/routes.cr</div>
```ruby
Amber::Server.configure do |app|
  pipeline :web do
    # pipelines...
  end

  routes :web do
    # other routes,,,
    websocket "/chat", ChatSocket
  end
end

Finally you will require an Amber JavaScript Client to connect with your server:

You can get amber.min.js from lib/amber/assets/js/amber.min.js

public/index.html
```markup

</div>


<div class="hint hint-warning"><p>To serve static files you must enable <code>:static</code> routes, please see <a href="../guides/routing/pipelines.md">pipelines</a>.</p>
</div>

Also see more detailed information about this in[ ](/docs/guides/controllers/cookies)[WebSockets](../guides/websockets/).