getting how many people are in a chat room in socket.io [duplicate]

For socket.io versions >= 1.0: Note that rooms became actual types with a .length property in 1.4, so the 1.4.x method should be stable from now on. Barring breaking changes to that type’s API, of course. To count all clients connected to ‘my_room’: 1.4+: var room = io.sockets.adapter.rooms[‘my_room’]; room.length; 1.3.x: var room = io.sockets.adapter.rooms[‘my_room’]; Object.keys(room).length; … Read more

How to use Socket.io with Next.js API Routes

The trick is to plug ‘socket.io’ into the http server only once, so checking every access to the api. Try something like this: ./pages/api/socketio.js import { Server } from ‘socket.io’ const ioHandler = (req, res) => { if (!res.socket.server.io) { console.log(‘*First use, starting socket.io’) const io = new Server(res.socket.server) io.on(‘connection’, socket => { socket.broadcast.emit(‘a user … Read more

socket.io: send message to specific room (client side)

You’ve pretty much figured it out. Only the server can emit to specific rooms, because it is only the server that is actually connected to multiple clients. In other words, clients aren’t connected to each other — the client-side library only manages communications between that one client and the server. So, if you want your client … Read more

socket.io – can’t get it to work, having 404’s on some kind of polling call

It looks like Socket.IO can’t intercept requests starting with /socket.io/. This is because in your case the listener is app — an Express handler. You have to make http be listener, so that Socket.IO will have access to request handling. Try to replace app.set( “ipaddr”, “127.0.0.1” ); app.set( “port”, 8080 ); with http.listen(8080, “127.0.0.1”); See … Read more

What is the disadvantage of using websocket/socket.io where ajax will do?

I think it would be more wasteful. For every connected client you need some sort of object/function/code/whatever on the server paired up with that one client. A socket handler, or a file descriptor, or however your server is setup to handle the connections. With AJAX you don’t need a 1:1 mapping of server side resource … Read more

Connect to Socket.IO server with specific path and namespace

On your server, don’t forget to specify the path as well: var io = require(‘socket.io’)(http, { path: ‘/myapp/socket.io’}); io .of(‘/my-namespace’) .on(‘connection’, function(socket){ console.log(‘a user connected with id %s’, socket.id); socket.on(‘my-message’, function (data) { io.of(‘my-namespace’).emit(‘my-message’, data); // or socket.emit(…) console.log(‘broadcasting my-message’, data); }); }); On your client, don’t confuse namespace and path: var socket = io(‘http://www.example.com/my-namespace’, … Read more

Socket.io custom client ID

You can create an array on the server, and store custom objects on it. For example, you could store the id created by Socket.io and a custom ID sent by each client to the server: var util = require(“util”), io = require(‘/socket.io’).listen(8080), fs = require(‘fs’), os = require(‘os’), url = require(‘url’); var clients =[]; io.sockets.on(‘connection’, … Read more