Chat-1
A robust Elixir library for building real-time chat applications. Provides a set of utilities for managing users, channels, and messages.
Installation
Add chat-1 to your list of dependencies in mix.exs:
elixir
def deps do
[
{:chat_1, "~> 0.1.0"}] end
Then run: bash mix deps.get
Usage
Here are a few examples showcasing how to use Chat-1:
1. Creating a new channel: elixir alias Chat1.Channel
case Channel.create("general") do {:ok, channel} ->
IO.puts "Channel created: #{channel.name}"->
IO.puts "Failed to create channel: #{reason}"end
2. Adding a user to a channel: elixir alias Chat1.User alias Chat1.Channel
Assume you have a user and a channel already created
user = %User{id: 1, name: "Alice"} channel = %Channel{id: 1, name: "general"}
case Chat1.Channel.add_user(channel.id, user.id) do {:ok, updated_channel} ->
IO.puts "User #{user.name} added to channel #{updated_channel.name}"->
IO.puts "Failed to add user: #{reason}"end
3. Sending a message to a channel: elixir alias Chat1.Message
Assume you have a user and a channel already created
user_id = 1 channel_id = 1 message_content = "Hello, everyone!"
case Chat1.Message.create(user_id, channel_id, message_content) do {:ok, message} ->
IO.puts "Message sent: #{message.content}"->
IO.puts "Failed to send message: #{reason}"end
4. Retrieving recent messages from a channel: elixir alias Chat1.Message
channel_id = 1 limit = 10
case Chat1.Message.recent_messages(channel_id, limit) do {:ok, messages} ->
Enum.each(messages, fn message ->
IO.puts "#{message.sender_id}: #{message.content}"
end)->
IO.puts "Failed to retrieve messages: #{reason}"end
5. Using pipes for a common sequence of operations: elixir alias Chat1.Channel alias Chat1.User alias Chat1.Message
Create a channel, add a user, and send a message
"general" |> Channel.create() |> case do {:ok, channel} ->
%User{id: 1, name: "Bob"}
|> User.create() # Assuming User.create exists
|> case do
{:ok, user} ->
Chat1.Channel.add_user(channel.id, user.id)
|> case do
{:ok, _updated_channel} ->
Chat1.Message.create(user.id, channel.id, "Hi from Bob!")
|> case do
{:ok, message} ->
IO.puts "Message sent successfully: #{message.content}"
{:error, reason} ->
IO.puts "Failed to send message: #{reason}"
end
{:error, reason} ->
IO.puts "Failed to add user to channel: #{reason}"
end
{:error, reason} ->
IO.puts "Failed to create user: #{reason}"
end->
IO.puts "Failed to create channel: #{reason}"end
Features
- Channel Management: Create, manage, and archive chat channels.
- User Management: Add, remove, and manage users within channels.
- Message Handling: Send, receive, and store chat messages efficiently.
- Real-time Support: Designed for integration with real-time communication systems.
- Extensible Architecture: Easily customizable and extensible to fit specific application needs.
- Robust Error Handling: Provides clear error messages for debugging and troubleshooting.
License
MIT
This package is part of the chat-1 ecosystem. For advanced features and enterprise-grade tools, visit: https://supermaker.ai/chat/