Question

I'm building a web app where users can connect to each other and chat. So each chat holds two users. I'm having some general questions concerning Pusher API, (http://pusher.com).

First question. Pusher lets you have 20 concurrent connection for free - does that mean I can have 10 ongoing chats at the same time? (Each chat is between two clients.)

Second question. How should I structure my channels? Should there be one channel per chat? (So 10 channels if there were 10 chats going on.) What would be the best way?

Was it helpful?

Solution

Does that mean I can have 10 ongoing chats at the same time?

You can actually have much more than that. you will have 10 if a person can speak ONLY to one person, however you would be limited by the number of messages in the plan.

How should I structure my channels? Should there be one channel per chat?

It depends if you care about knowing : who's online at all time, and if you want to reduce the number of presence messages spent by Pusher to optimise your plan.

Option 1 :

Each user subscribes to a channel named after him and listens to its events.

user1 will subscribe to presence-user1 channel and bind to the *message_received* event on this channel :

var channel = pusher.subscribe('user1');

channel.bind('message_received', function(message){ 
    console.log('Message received : ');
    console.log(message);
});

When user1 wants to contact user2 they simply need to trigger a *message_received* and pass to it the message and some metadata :

var channel2 = pusher.subscribe('user2');

channel2.trigger('message_received',{
    message:'hey', 
    sender:'user1',
    sent_on:helpers.date() 
});

Pusher have very good documentation and a very neat api here's the page where they talk about events

Since we're using a presence channel here you can get presence information about the users you care about as well.

[EDIT] Note that with this way all users subscribed to the user1 channel will be notified about the messages sent on his channel and this a suboptimal way to do it ! A better way to do it would be to create channels between each two users. There are many ways to architect your app, but I think the previous code should give you a basic idea.

Option 2 :

Create a room 'presence-all'. To which every user connects, then send private messages via 'namespaced events' so for example, user1 would bind to user1:message_received on the presence_all channel. The problem with that approach is the overhead, however if you're not expecting heavy traffic you can use this one as it is much more straight forward to code.

Or create a hybrid approach between both one for presence only and one for exchanging messages. You should check [https://pusher.tenderapp.com/kb/accountsbillingplanspricing/how-is-my-message-count-calculated][this] Pusher explain in detail how they price their messaging.

I hope this helps !

OTHER TIPS

Does that mean I can have 10 ongoing chats at the same time?

Yes, according to their support forum post, "How are connections counted?".

How should I structure my channels? Should there be one channel per chat?

In the most basic setup, there is one channel per chat. But in most cases, there are additional channels used for communicating the presence of other users and other functions. A good place to start is their Tutorial for a Realtime Chat Widget.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top