Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nextjs socket.io

/*
Install SocketIO for Creating Server
npm i socket.io

Install SocketIO-Client for Creating Client 
npm i socket.io-client
*/

// Socket Server
// pages/api/socket.js ------------------------------
import { Server } from 'socket.io'

const SocketHandler = (req, res) => {
  if (res.socket.server.io) {
    console.log('Socket is already running')
  } else {
    console.log('Socket is initializing')
    const io = new Server(res.socket.server)
    res.socket.server.io = io

    io.on('connection', socket => {
      socket.on('input-change', msg => {
        socket.broadcast.emit('update-input', msg)
      })
    })
  }
  res.end()
}

export default SocketHandler
// --------------------------------------------------

// Socket Client
// pages/home.js -----------------------------------
import { useEffect, useState } from 'react'
import io from 'socket.io-client'
let socket;

const Home = () => {
  const [input, setInput] = useState('')

  useEffect(() => {
      socketInitializer();
      return () => {
        console.log("This will be logged on unmount");
      }
    })

  const socketInitializer = async () => {
    await fetch('/api/socket');
    socket = io()

    socket.on('connect', () => {
      console.log('connected')
    })

    socket.on('update-input', msg => {
      setInput(msg)
    })
  }

  const onChangeHandler = (e) => {
    setInput(e.target.value)
    socket.emit('input-change', e.target.value)
  }

  return (
    <input
      placeholder="Type something"
      value={input}
      onChange={onChangeHandler}
    />
  )
}

export default Home;
// ----------------------------------------------
Comment

PREVIOUS NEXT
Code Example
Javascript :: angular output 
Javascript :: how to remove a part of a string javascript 
Javascript :: how to creat a function 
Javascript :: adonis count with where 
Javascript :: get first property from object javascript 
Javascript :: datatable numeric format 
Javascript :: canada postal code regex 
Javascript :: javascript redirect to another page 
Javascript :: javascript redirect function 
Javascript :: redirect with javascript to another page 
Javascript :: javascript get day of year 
Javascript :: saving json file python 
Javascript :: javascript open in new window not tab 
Javascript :: URL scheme "localhost" is not supported. 
Javascript :: reactjs link props 
Javascript :: hello world in jsp 
Javascript :: express js cors 
Javascript :: check overflow react 
Javascript :: nodejs readdir 
Javascript :: localstorage save array 
Javascript :: how to add another schema id on mongodb 
Javascript :: how to create a button with react 
Javascript :: how to change active tab jquery 
Javascript :: get middle of string js 
Javascript :: type of variable js 
Javascript :: jQuery UI Sortable, then write order into a database 
Javascript :: ReferenceError: http Server is not defined 
Javascript :: calling javascript functions from unity scripts 
Javascript :: last item in object javascript 
Javascript :: rgb to hex js 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =