using PubNub for multi users chatting

守給你的承諾、 提交于 2019-12-24 03:29:13

问题


I'm designing an application that using PubNub for real-time message. I'm facing with an issue about 1-to-1 chatting. This is my scenario :

I have an user A, so if A wants to receive all messages from another, A must subscribe a public channel, called PUB channel.

User B know the public channel of A, so B or another can send messsage to this channel. Right now that fine.

BUT when i open the app, i want to display the list of users that i've messaged or offline messages that a NEW person sent me.

I can get all messages from my public channel. but how to filter with the user? in case of a lot of messages, this is so difficult.

With all of them , i only want get messages of me and C, or me and B...etc Anyone have ideas for this? Thanks,


回答1:


PubNub Multi-user Chat and Communication

Each user has their own dedicated channel. Bob has the "bob12345" channel and Sally has "sally12345" channel. When users want to speak to each other, they send message directly to the owner's channel name. Bob will pubnub.publish({...}) on Sally's channel.

pubnub.publish({ 
    channel : 'sally12345', 
    message : { from: 'bob12345', message: 'Hi!' }
});

Note that you will want to grant global write access using PubNub Access Manager to Grant Write-only access globally. Also grant read-only access to the owner of the channel.

Each user should always have their messages stored locally on their device or load all history and save.

How to Load a Conversation between two users only

If you want to load the messages from a specific user's conversation, you will need to publish to a side channel to index the messages for future retrieval. You can do this by publishing to a private side channel that is a concatenation of Bob's and Sally's channel name.

// Publish a second time to index the message in a side channel.
pubnub.publish({ 
    channel : 'history-sally12345-bob12345', 
    message : { from: 'bob12345', message: 'Hi!' }
});

Now you can load history for this specific user.

// Load history for dedicated conversation.
pubnub.history({ 
    channel  : 'history-sally12345-bob12345', 
    callback : function(messages) { 
        console.log(messages);
    }
});


来源:https://stackoverflow.com/questions/23038446/using-pubnub-for-multi-users-chatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!