Amber Framework
WebsiteBlogGithubDiscord
  • Introduction
  • Getting Started
  • Guides
    • Overview
    • Installation
    • Create New App
    • Directory Structure
    • Configuration
    • Docker
    • Controllers
      • Sessions
      • Request & Response Objects
      • Halt!
      • Respond With
      • Params
      • Cookies
      • Filters
      • Flash
      • Redirection
      • CSRF
    • Views
      • Basic View Helpers
      • Form Builder
    • Models
      • Granite
        • Granite's README
        • Migrations
        • Validations
        • Callbacks
        • Associations
        • Querying
        • Bulk Insertions
      • Jennifer
        • Jennifer Docs
        • Migrations
        • Models
    • Routing
      • Pipelines
      • Routes
    • Websockets
      • Channels
      • Sockets
      • JavaScript Client
    • Mailers
    • Testing
      • System Tests
  • Deployment
    • Manual Deploy
    • Digital Ocean
    • Heroku
    • Dokku
  • CLI
    • New
    • Recipes
    • Plugins
    • Generate
    • Database
    • Watch
    • Routes
    • Exec
    • Encrypt
  • Examples
    • Amber Auth
    • Crystal Debug
    • Minimal Configuration
  • Cookbook
    • From Scratch
    • Hello World
    • CORS
    • File Download
    • File Upload
    • Cookies
    • Authenticate
    • JSON API
    • JSON Mapping
    • WebSocket Chat
  • Troubleshooting
  • In Production
  • Contributing
  • Code of Conduct
  • HAVE A QUESTION?
    • Join the Discord
    • Follow on Twitter
    • Submit an issue
Powered by GitBook
On this page
  • Introduction
  • Example Usage
  • Add a new Route
  • Send messages from controllers (or anywhere else)
  1. Guides
  2. Websockets

Sockets

Introduction

Socket structs are the objects that are stored in memory per connection and retain the persistent communication.

Sockets define one public method on_connect which can contain functionality that should run when a user connects, including authentication. This methods should return a Bool. If true the socket will remain open. If false the socket will be closed immediately.

An Amber::WebSockets::ClientSocket instance has both cookies and session getters and are available from within the on_connect method.

Socket structs also map topics to the channels they will connect with.

Example Usage

A socket can be generated by calling amber g socket Chat

struct ChatSocket < Amber::WebSockets::ClientSocket
  channel "chat_room:*", ChatRoomChannel

  def on_connect
    # self.session and self.cookies available here
    # do authentication here like
    # !self.session[:user_id].nil?

    # returning true accept all connections
    true
  end
end

Add a new Route

A new route needs to be added so that a handshake can be established with the server. Notice how after \chat, the struct ChatSocket that was created above is mapped to that route.

Amber::Server.configure do |app|
  pipeline :web do
    # pipelines...
  end

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

Send messages from controllers (or anywhere else)

Amber::WebSockets::ClientSocket provides a public class method broadcast for publishing messages to all subscribers of a topic from within controllers or anywhere else in your application.

class HomeController < ApplicationController
  def index
    ChatSocket.broadcast("message", "chat_room:123", "message_new", {"message" => "A new visitor!"})
    render("index.slang")
  end
end
PreviousChannelsNextJavaScript Client

Last updated 2 years ago