Quantcast
Channel: Ionic Forum - Latest topics
Viewing all articles
Browse latest Browse all 70435

Ionic 4 chat application and how to send message to particular user?

$
0
0

@sasatozovic wrote:

I created application in Ionic 4, and for backend I use Lumen. Application should have chat page and in that purpose I add Redis, Socket.io and nodejs. I successfully created public room, and chat between users in that room works. Problem is how to send private message for user, how to initialize users for their private room.

This is how I created public room:

constructor(private socket: Socket) {

        this.getMessages().subscribe(message => {
            this.messages.push(message);
        });

    }

    getMessages() {
        const observable = new Observable(observer => {
            this.socket.on('message', (data) => {
                observer.next(data);
            });
        });
        return observable;
    }

I send message from Lumen application and Redis:

public function sendMessage()
{
    $redis = Redis::Connection();
    $sendMessage = json_encode(['user' => 'John Doe', 'text' => 'Some message, text', 'channel' => 'message']);
    $redis->publish('add-message', $sendMessage);
}

And my node server is:

let express = require('express');
let app = express();
let http = require('http').Server(app);
let redis = require('redis');
let client = redis.createClient("redis://127.0.0.1:6379");

let io = require('socket.io')(http);

app.use('/', express.static('www'));

http.listen(3000, '192.168.10.10', function(){
    console.log('listening on *:3000');
});

client.on('message', function(chan, msg) {
    let data = JSON.parse(msg);
    io.sockets.emit(data.channel, msg);
});

client.subscribe('add-message');

Bottom line everyone who is subscribed on ‘message’ channel will get message. Problem is that I subscribe user on channel when came to chat page. I don’t know how to subscribe user, and when, on channel where some another user send him message.Also how that sender user to create new room, I suppose to use id of users for room name (per instance user1_user2).

Does anyone know how I can solve this problem? I don’t know even I described well.

Thanks in advance

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 70435

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>