Git Product home page Git Product logo

slchat's Introduction

SLChat

Language License TravisCI Codebeat

SLChat is a simple extension for Kitura-WebSocket. Allows you to integrate chat system with your client.

๐Ÿ”ง Installation

Add the following dependency to your Package.swift file:

.package(url: "https://github.com/shial4/SLChat.git", from: "0.1.1"),

๐Ÿ’Š Usage

1 Import

It's really easy to get started with the SLChat library! First you need to import the library, by adding this to the top of your Swift file:

import SLChat

2 Initialize

The easiest way to setup SLChat is to create object for example in your main.swift file. Like this:

let slChat = SLService<Client>()

Perfectly works with Kitura.

WebSocket.register(service: SLService<Client>(), onPath: "slchat")

3 Configure

SLService instance require your Client model. If you won't use it, you can simply declare an empty struct for that

struct Client: SLClient {}
let chat = SLService<Client>()

SLClient Protocol

Every function in this protocol is optional. It means SLClient provide default implementation. However, you are allowed to override it by your own. Why do that? To provide additional functionality. For example: data base storage for your message history, handle Data messages, provide recipients for status messages like connected or disconnected. And many more!

extension SLClient {
    static func receivedData(_ message: Data, from: WebSocketConnection) -> Bool { return false }
    static func sendMessage(_ message: SLMessage, from client: String) -> [String] { return message.recipients }
    static func statusMessage(_ command: SLMessageCommand, from client: String) -> [String]? { return nil }
}

Sending message via sendMessage protocol method. Provide SLMessage object which hold recipients parsed from socket message. This can be chat room ids or simple client ids to which message should be delivered.

Recipients are strings defined by you. Thanks to that you can identify your target clients to which message should be delivered. They can be simple other clients id or room id.

SLService

Is very simple in the way it works. First of all what you should know is: Message sent to clinet looks like: M;MESSAGE-OWNER-ID;{MESSAGE-RECIPIENT-ID, CAN_BE_CHAT_ROOM_ID, CAN_BE_OTHER_CLIENT_ID};My message content sent to others First character in this case M is the type of message. Followed by client id responsible for sending this message with recipients placed inside {} and the last part is message content. All parts joined by ; This message model will be delivered to your client application. ``

Status messages (connected or disconnected) are different, they simply have empty recipient closure {}. If you receive message: C;MESSAGE-OWNER-ID;{}; Simply all clients related with message owner should be notify.

Beside receiving messages, you will send some as well! Your message should look like: M{RECIPIENT_1;RECIPIENT_2;RECIPIENT_3}My message content sent to others Similar to recived message, first character describe type of message. Followed by recipients places inside {} separated by ; with the last component is the message content.

To open socket conection from client register your WebSocket on: ws:// + host + /slchat?OWNER-ID SLChat uses path components to send owner id.

โญ Contributing

Be welcome to contribute to this project! :)

โ“ Questions

You can create an issue on GitHub.

๐Ÿ“ License

This project was released under the MIT license.

slchat's People

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.