Maxy.fr

Envoi d’informations avec socket.io

Un point important à savoir lorsque l’on utilise socket.io, c’est les différentes façons d’envoyer des informations entre le serveur et les utilisateurs.

  • La première est d’envoyer les données à l’utilisateur qui « les demandes », et lui seul :
io.sockets.on('connection', function(socket){
 
socket.emit('nom', donnees);
 
});
  • La seconde est d’envoyer les données à tous les utilisateurs sauf celui qui en a fait la demande :
io.sockets.on('connection', function(socket){
 
socket.broadcast.emit('nom', donnees);
 
});
  • Enfin la troisième façon de faire est d’envoyer les informations à tous les utilisateurs sans exception :
o.sockets.emit('nom', donnees);

 

Categories : Javascript, Node.JS

By Guillaume - 11/05/2013 at 11:25 pm

Shar this article :

Similar articles

Easily add a dual-slider to your HTML forms

When designing HTML forms, you may quickly find yourself limited by the basic form elements provided. For example, if you use sliders with the <input type="range"> element, they only allow the use of a single slider per element. However, there may be times when you need a field with two...

Avoiding slowdowns of a JavaScript counter on an inactive tab

If you're trying to create a Javascript counter that increments every second, you may notice that it works correctly when you're on the active tab but experiences slowness when the tab is not active. This can lead to a desynchronization between the counted seconds and real-time. Here's a simple...

Finding and Stopping a Lingering Node.js Instance

If you've been playing around with Node.js, you've probably run a Node.js file and left it running in the background. You may be accustomed to using the shortcut ctrl + c (cmd + c) or ctrl + z (cmd + z) to suspend or stop its process. However, if you've closed the terminal with your SSH connection...