Search
 
SCRIPT & CODE EXAMPLE
 

HTML

socket io script

<script src="/socket.io/socket.io.js"></script>
<script>
  const socket = io();
</script>
Comment

socket.io node

/*The following example attaches socket.io to a plain Node.JS HTTP server listening on port 3000.*/

const server = require('http').createServer();
const io = require('socket.io')(server);
io.on('connection', client => {
  client.on('event', data => { /* … */ });
  client.on('disconnect', () => { /* … */ });
});
server.listen(3000);



//Standalone

const io = require('socket.io')();
io.on('connection', client => { ... });
io.listen(3000);
//Module syntax
import { Server } from "socket.io";
const io = new Server(server);
io.listen(3000);
                               
                               
                               
//In conjunction with Express
//Starting with 3.0, express applications have become request handler functions that you pass to http or http Server instances. You need to pass the Server to socket.io, and not the express application function. Also make sure to call .listen on the server, not the app.

const app = require('express')();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
io.on('connection', () => { /* … */ });
server.listen(3000);
                               
                               
//In conjunction with Koa
//Like Express.JS, Koa works by exposing an application as a request handler function, but only by calling the callback method.

const app = require('koa')();
const server = require('http').createServer(app.callback());
const io = require('socket.io')(server);
io.on('connection', () => { /* … */ });
server.listen(3000);
                               
                               
                               
//In conjunction with Fastify
//To integrate Socket.io in your Fastify application you just need to register fastify-socket.io plugin. It will create a decorator called io.

const app = require('fastify')();
app.register(require('fastify-socket.io'));
app.io.on('connection', () => { /* … */ });
app.listen(3000);
     
Comment

socket io nodejs

import { Server } from "socket.io";

const io = new Server(3000);

io.on("connection", (socket) => {
  // send a message to the client
  socket.emit("hello from server", 1, "2", { 3: Buffer.from([4]) });

  // receive a message from the client
  socket.on("hello from client", (...args) => {
    // ...
  });
});
Comment

socket io nodejs

const express = require('express');const app = express();const http = require('http');const server = http.createServer(app);const { Server } = require("socket.io");const io = new Server(server);app.get('/', (req, res) => {  res.sendFile(__dirname + '/index.html');});io.on('connection', (socket) => {  console.log('a user connected');});server.listen(3000, () => {  console.log('listening on *:3000');});
Comment

PREVIOUS NEXT
Code Example
Html :: mailto html multiple addresses 
Html :: menu vertical html 
Html :: how open url in html in new window with custom size 
Html :: best html emmet extension for vs code 
Html :: select set selected value html 
Html :: import html display 
Html :: what is markup in html 
Html :: how to make windows pop up in html 
Html :: change source of iframe attribute target 
Html :: html2pdf pagebreak option 
Html :: replit iframe 
Html :: html set all text size 
Html :: composer sail alias 
Html :: free landing page template html5 
Html :: vue js v-for array index 
Html :: fix form refresh sites html 
Html :: bootstrap 5 card common 
Html :: game code in html 
Html :: how to make squares in html 
Html :: how to create seprate flex box for adding items into the box 
Html :: html input attributes 
Html :: html radio input 
Html :: embed png in html 
Html :: insert html in html page 
Html :: html select option text color 
Html :: html popup form 
Html :: html5 exmaple 
Html :: contact us form html send email 
Html :: html li tag 
Html :: a page with html cheat sheet 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =