sm-chat
A streamlined Elixir library for building robust and scalable chat applications. It provides a set of utilities for managing conversations, users, and messages with ease.
Installation
To install sm-chat, add it as a dependency to your mix.exs file:
elixir
def deps do
[
{:sm_chat, "~> 0.1.0"} # Replace with the actual version] end
Then, run mix deps.get to fetch the dependencies.
Usage Examples
Here are a few examples demonstrating how to use sm-chat:
1. Creating a new chat room: elixir alias SmChat.Room
case Room.create("General Discussion") do {:ok, room} ->
IO.puts "Room created: #{room.name}"->
IO.puts "Failed to create room: #{reason}"end
This example shows how to create a new chat room named "General Discussion". The Room.create/1 function returns either {:ok, room} on success or {:error, reason} on failure, allowing for easy error handling.
2. Adding a user to a chat room: elixir alias SmChat.User alias SmChat.Room
Assuming we have a room and a user already created
room = %Room{id: 1, name: "General Discussion"} user = %User{id: 101, name: "Alice"}
case SmChat.Room.add_user(room, user) do {:ok, updated_room} ->
IO.puts "User added to room: #{updated_room.name}"->
IO.puts "Failed to add user: #{reason}"end
This demonstrates adding a user to an existing chat room. Pattern matching on the result allows you to handle both success and failure scenarios.
3. Sending a message to a chat room: elixir alias SmChat.Message alias SmChat.User alias SmChat.Room
Assuming we have a room and a user already created
room = %Room{id: 1, name: "General Discussion"} user = %User{id: 101, name: "Alice"}
case Message.create(room, user, "Hello, everyone!")
|> Message.deliver() do->
IO.puts "Message sent: #{message.content}"->
IO.puts "Failed to send message: #{reason}"end
This example shows how to send a message to a chat room. It leverages the pipe operator (|>) to chain the creation and delivery of the message, resulting in concise and readable code.
4. Retrieving recent messages from a chat room: elixir alias SmChat.Room alias SmChat.Message
Assuming we have a room already created
room = %Room{id: 1, name: "General Discussion"}
case Message.get_recent(room, limit: 10) do {:ok, messages} ->
Enum.each(messages, fn message ->
IO.puts "#{message.sender.name}: #{message.content}"
end)->
IO.puts "Failed to retrieve messages: #{reason}"end
This demonstrates retrieving the last 10 messages from a specific chat room.
5. Handling user presence (example - requires custom integration with presence tracking library): elixir alias SmChat.User
Example of updating user status (requires integration with a presence library like Phoenix.Presence)
defmodule MyApp.UserPresence do use GenServer
def start_link(user_id) do
GenServer.start_link(__MODULE__, user_id, name: via_tuple(user_id))end
defp via_tuple(user_id) do
{:via, Registry, {SmChat.PresenceRegistry, user_id}}end
def init(user_id) do
{:ok, user_id}end
def handle_info(:online, state) do
# Update user status in database or other persistent store
IO.puts "User #{state} is now online"
{:noreply, state}end
def handle_info(:offline, state) do
# Update user status in database or other persistent store
IO.puts "User #{state} is now offline"
{:noreply, state}end end
This example shows how to handle user presence events. Note that this requires integration with a separate presence tracking library like Phoenix.Presence. The MyApp.UserPresence GenServer would need to be notified when the user's presence changes. This example highlights the extensibility of sm-chat for integrating with other systems.
Feature Summary
- Room Management: Create, update, and delete chat rooms.
- User Management: Add and remove users from chat rooms.
- Message Handling: Send, receive, and store messages.
- Extensible Architecture: Designed for easy integration with other systems.
- Robust Error Handling: Provides clear error messages for debugging.
License
MIT
This package is part of the sm-chat ecosystem. For advanced features and enterprise-grade tools, visit: https://supermaker.ai/chat/