Socket.io

4 min. read

Socket.io is a realtime JavaScript Library

Introduction to Socket.io

History

Examples

// sending to sender-client only
socket.emit(‘message’, “this is a test”);

// sending to all clients, include sender
io.emit(‘message’, “this is a test”);

// sending to all clients except sender
socket.broadcast.emit(‘message’, “this is a test”);

// sending to all clients in ‘game’ room(channel) except sender
socket.broadcast.to(‘game’).emit(‘message’, ‘nice game’);

// sending to all clients in ‘game’ room(channel), include sender
io.in(‘game’).emit(‘message’, ‘cool game’);

// sending to sender client, only if they are in ‘game’ room(channel)
socket.to(‘game’).emit(‘message’, ‘enjoy the game’);

// sending to all clients in namespace ‘myNamespace’, include sender
io.of(‘myNamespace’).emit(‘message’, ‘gg’);

// sending to individual socketid
socket.broadcast.to(socketid).emit(‘message’, ‘for your eyes only’);

// list socketid
for (var socketid in io.sockets.sockets) {}
OR
Object.keys(io.sockets.sockets).forEach((socketid) => {});

Official Examples:
https://github.com/socketio/socket.io/tree/master/examples

Angular + Socket.io: https://www.techiediaries.com/angular-9-realtime-chat-example-with-nodejs-socketio-and-websocket/

Video Streaming:
https://gabrieltanner.org/blog/webrtc-video-broadcast

Rooms:
http://psitsmike.com/2011/09/node-js-and-socket-io-chat-tutorial/
http://psitsmike.com/2011/10/node-js-and-socket-io-multiroom-chat-tutorial/
https://gist.github.com/crtr0/2896891
https://socket.io/docs/v3/rooms/
https://socket.io/docs/v3/namespaces/index.html
https://www.alxolr.com/articles/working-with-socket-io-dynamic-namespaces
https://dev.to/wpreble1/socket-io-namespaces-and-rooms-d5h
https://hub.packtpub.com/adding-real-time-functionality-using-socketio/
https://www.tutorialspoint.com/socket.io/socket.io_rooms.htm
https://stackoverflow.com/questions/41322878/dynamic-rooms-in-socket-io

How to Guides

How To: Make a Unity Multiplayer Game using NodeJS

Testing Socket.io with Moach and Chai
https://alexzywiak.github.io/testing-socket-io-with-mocha-and-chai/index.html

How to: Run NodeJS with Socket.io on AWS EB
https://stackoverflow.com/questions/47584103/socket-io-in-aws-elasticbeanstalk-node

https://www.purplesquirrels.com.au/2017/09/getting-websockets-working-with-node-socket-io-and-elastic-beanstalk/

https://aws.amazon.com/blogs/database/how-to-build-a-chat-application-with-amazon-elasticache-for-redis/

How to: Test Socket.io with Artillery.io
https://itnext.io/websocket-load-testing-with-artillery-io-b8b7ecbcd7ed

https://dev-tester.com/battle-test-your-api-with-artillery-load-testing/

http://domwatson.codes/2021/06/socketio-load-testing.html

https://www.findbestopensource.com/product/hassy-socketio-load-test-artillery-example

https://dev.to/brpaz/load-testing-your-applications-with-artillery-4m1p

https://www.testim.io/blog/artillery-load-testing-introduction-see-how-your-code-scales/

https://www.habilelabs.io/install-artillery-load-testing-tool/

https://socket.io/how-to/check-the-latency-of-the-connection

References

Real-time Application with socket.io/Firebase

Socket.io

Repo
https://github.com/EcutDavid/socket.io-firebase-quickstart

https://socket.io/docs/v3/emit-cheatsheet/index.html

https://stackoverflow.com/questions/24224287/list-of-socket-io-events

https://socket.io/docs/v3/client-api/index.html

https://socket.io/docs/v3/server-api/index.html

Stream a video using Express.js and Socket.io

Integrate Socket.IO with Node.Js + Express

https://socket.io/docs/v3/listening-to-events/

https://socket.io/demos/whiteboard/
https://github.com/socketio/socket.io/tree/master/examples/whiteboard
https://github.com/alexishevia/blogExamples/blob/cors/backend/app.js

https://socket.io/docs/v4/performance-tuning/