sdelements / lets-chat

Self-hosted chat app for small teams
http://sdelements.github.io/lets-chat
MIT License
9.77k stars 1.58k forks source link

Websocket API? #587

Open coxley opened 9 years ago

coxley commented 9 years ago

The REST API seems great to fit most use cases, just wondering if there's a way to perhaps get a consistently updated stream of chats for a room since you probably wouldn't want to just keep issuing GETs and risk being out of order.

hhaidar commented 9 years ago

Yup, it's not documented as we're still ironing it out. Basically you listen to a socket.io event for messages:new after you send the server room:join.

Here's an example:

'use strict';

var socket = require('socket.io-client');

var io = socket(YOUR_SERVER_URL_GOES_HERE, {
    autoConnect: true,
    reconnect: true,
    query: {
        token: YOUR_TOKEN_GOES_HERE
    }
});

io.on('connect', function() {

    console.log('connected!');

    io.emit('rooms:list', function(rooms) {

        console.log('Found', rooms.length, 'rooms');

    });

    io.emit('rooms:join', YOUR_ROOM_ID_GOES_HERE, function(room) {

        console.log('joined', room.name);

    });

    io.on('messages:new', function(message) {
        console.log('<' + message.owner.username + '>:', message.text);
    });

});
hhaidar commented 9 years ago

I should also add that you can connect very easily with any language/platform that has a socket.io client library (Ruby, Python, etc).

coxley commented 9 years ago

Are the events/commands documented to for if you wanted to send messages?

hhaidar commented 9 years ago

@coxley not at this moment.

If you want to create a new message, something like this would work:

io.emit('messages:new', {
    room: SOME_ROOM_ID,
    text: SOME_TEXT
});
coxley commented 9 years ago

Could you mimic a user like that?

hhaidar commented 9 years ago

What do you mean?

Whatever token you give it will always belong to a user.