Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Socket.io private message notification

Hey there stackoverflow! I am developing a community with Laravel3 right now I am trying to achieve some Facebook style private messaging.

I did the coding part, but me and my friends are so agree about adding real-time message notifications. I did my research, I cannot say I find a good article about this, some of them starts with oh you know everything about matrix so lets socketsocketsocketsocketsocket I am so confused how to start, where to start, what is this anyway, many of this say go with MongoDB never use MySQL again. Dude what the heck? I am using MySQL I created a nice private message system in Laravel, I want to add real-time notify!

All I want to do is

  1. UserA sends a message to UserB
  2. Message inserted into privmsg table.
  3. Sockets or whatever you suggest, tells UserB's browser there is +1 new message from UserA
  4. UserB sees there is one unread message without refresh his page and click to read it .

How can I achieve this? Socket.io best choice for that? if so how can I use it? any snippets would be so great! or tutorial about my situation :)

I would really grateful

like image 910
T. Cem Yılmaz Avatar asked Jan 26 '13 07:01

T. Cem Yılmaz


1 Answers

You suffer from the problem of thinking "X is always better than Y so just always use X". There's probably a name for it... maybe even a book or two written. Who knows. Let's start with your first question:

what is this shit anyway, many of this say "go with MongoDB never use MySQL again." Dude what the heck?

You should stop spending time with whomever said that. MySQL and MongoDB are database systems for two very different types of databases. They are often referred to as table-based and document-based. With MySQL (any many other databases that utilize SQL... and probably some that don't), your data is stored in a set of relational tables outlined by a very specific schema. Each record in this table conforms to a specific set of fields with a specific set of types. This type of database is perfect for many kinds of data.

MongoDB is the document-based variety of databases, commonly referred to as "NoSQL" (meaning not-only-SQL). Each "document" can have a whole structure, complete with nodes that have children and grand children. Each document can have its own distinct set of data. Documents are stored in "collections". This type of database has some advantages... it can be quite fast for certain types of operations. This being said, it is terrible for other things, such as when you have a bunch of data that is all identical in type. Data aggregation is very slow on databases like these (but it is getting better all the time!).

The point I'm trying to make is that MySQL and MongoDB are just different tools, designed for different jobs. Stop pounding a nail with your screwdriver just because your friend tells you that screws are better than nails.

All I want to do is: UserA sends a message to UserB; Message inserted into privmsg table.; Sockets or whatever you suggest, tells UserB's browser there is +1 new message from UserA; UserB sees there is one unread message without refresh his page and click to read it .

Again, pick the tool for the job. Knowing what your tools are is a good start. Socket.IO is meant to set up a communications channel between a server and a client. It provides web-socket-like functionality, commonly used between Node.js servers and web browsers (but can be used in other contexts as well!). Its two main features are that it provides fallback transports when Web Sockets are not available (making it compatible with old browsers), and it wraps up an event messaging system in some nice and simple calls. You don't have to worry about the underlying communication. Just emit and event on one end, and it is fired on the other. Simple.

For the actual communication between your servers and the browser, Socket.IO is an excellent choice. It provides near-real-time communication. However, Socket.IO isn't just some magic that will solve all of your problems for you. It would be useless to almost everyone if it were.

Since your messages need to persist, storing them in a database is a good idea. What I would do:

  1. On message send, insert a copy into the database
  2. On insert, fire a notice over your pub/sub to the other servers in your cluster
  3. Any server that has a connection to the user getting the message will see this notice from the other server.
  4. That server will load the user's message data out of the database and emit it over Socket.IO

You want a tutorial? The example on the Socket.IO home page is quite good: http://socket.io/

like image 123
Brad Avatar answered Oct 22 '22 14:10

Brad